- 使用
evictUbiquitousItem(at url:)
- 见点#1。不过,我不确定你为什么想这样做。你不能停止访问本地副本第二个开关关闭?
- 我不认为它可以通过编程方式禁用。从积极的方面来看,如果你想利用可能性禁用iCloud的用户,它已经在那里。 iCloud的是应该通过设置被禁用应用程序,你真正需要做的是通过听
NSUbiquityIdentityDidChangeNotification
UPDATE处理应用程序内的事实
阿明Negm - 阿瓦德指出evictUbiquitousItem(at url:)
力量重新加载,所以本地副本不会被永久删除。然而,我只是出于好奇而做了一些小测试,并没有发现这种情况。以下测试:
func runTest(ubiURL: URL) {
self.query.searchScopes = [NSMetadataQueryUbiquitousDocumentsScope]
self.query.predicate = NSPredicate(format: "%K like '*'", NSMetadataItemFSNameKey)
NotificationCenter.default.addObserver(self, selector: #selector(self.metadataQueryDidUpdate(_:)), name: NSNotification.Name.NSMetadataQueryDidUpdate, object: self.query)
self.query.start()
self.query.enableUpdates()
let fileURL = ubiURL.appendingPathComponent("Documents/file.txt")
FileManager.default.createFile(atPath: fileURL.path, contents: "Hello".data(using: .utf8))
do {
try FileManager.default.startDownloadingUbiquitousItem(at: fileURL)
} catch {
print("startDownloadingUbiquitousItem: \(error.localizedDescription)")
}
}
var updateCount = 0
func metadataQueryDidUpdate(_ notification: Notification) {
print("######################")
print("update #\(updateCount)")
for file in query.results as! [NSMetadataItem] {
guard let fileURL = file.value(forAttribute: NSMetadataItemURLKey) as? URL, let fileStatus = file.value(forAttribute: NSMetadataUbiquitousItemDownloadingStatusKey) as? String else {
print("Invalid item!")
return
}
if fileStatus == NSMetadataUbiquitousItemDownloadingStatusCurrent {
print("URL to evict: \(fileURL)")
do {
try FileManager.default.evictUbiquitousItem(at: fileURL)
print("Eviction result: successful")
} catch {
print("evictUbiquitousItem: \(error.localizedDescription)")
}
}
print("File exists at URL: \(FileManager.default.fileExists(atPath: fileURL.path))")
}
updateCount = updateCount + 1
}
墓内:
ubiURL is file:///private/var/mobile/Library/Mobile%20Documents/iCloud~com~example~blabla3/
######################
update #0
URL to evict: file:///private/var/mobile/Library/Mobile%20Documents/iCloud~com~example~blabla3/Documents/file.txt
evictUbiquitousItem: The file “file.txt” couldn’t be saved in the folder “blabla”.
File exists at URL: true
######################
...
update #3
URL to evict: file:///private/var/mobile/Library/Mobile%20Documents/iCloud~com~example~blabla3/Documents/file.txt
Eviction result: successful
File exists at URL: true
######################
update #4
File exists at URL: false
(no further updates followed)
我不认为远程文件最终会在设备上,除非startDownloadingUbiquitousItemAtUrl:
被称为与拆迁户文件的URL。不过,不确定这种行为是否可以依赖。
我认为'evictUbiquitousItemAtURL:error:'不会永久删除本地副本,而是强制重新加载。我认为在iCloud中仍然有版本的情况下,根本不可能删除本地副本。 –
@Amin Negm-Awad查看我的更新回答 – crizzis