UIDocument
- 如何使用icloud 同步文件
- 如何添加文件到icloud
- 如何获取icloud中文件列表
- 如何删除icloud中文件
- 多个设备同时打开一个document 如何同步
- 多个设备如何同步document列表
- 如何处理冲突
1. 基本配置
首先添加icloud capability,这个需要开发者账号, 然后添加一个icloud container id。
2. 添加数据
想把一个文件数据放到icloud上,要先检查icloud 是否可用
- 检查icloud是否已经可用,并获取到icloud container url
guard FileManager.default.ubiquityIdentityToken != nil else {
print("⛔️ iCloud isn't enabled yet. Please enable iCloud and run again.")
return nil
}
// Dispatch to a global queue because url(forUbiquityContainerIdentifier:) might take a nontrivial
// amount of time to set up iCloud and return the requested URL
//
DispatchQueue.global().async {
if let url = FileManager.default.url(forUbiquityContainerIdentifier: containerIdentifier) {
DispatchQueue.main.async {
self.containerRootURL = url
}
return
}
print("⛔️ Failed to retrieve iCloud container URL for:\(containerIdentifier ?? "nil")\n"
+ "Make sure your iCloud is available and run again.")
}
上面代码检查icloud 功能是否开启,并获取icloud container 容器的url 地址。
- 下面要封装数据,然后放到icloud container里面
需要自定义类继承UIDocument,然后通过操作document来实现文件的增删改
let document = Document(fileURL: fileURL)
document.save(to: fileURL, for: .forCreating) { _ in
document.close { success in
if !success {
print("Failed to close the document: \(fileURL)")
}
completionHandler?(success)
}
}
func removeDocument(at fileURL: URL) {
DispatchQueue.global().async {
NSFileCoordinator().coordinate(writingItemAt: fileURL, options: .forDeleting, error: nil) { newURL in
do {
try FileManager.default.removeItem(atPath: newURL.path)
} catch let error as NSError {
print(error.localizedDescription)
}
}
}
}
要看看文件是否已经添加成功,可以在设置->iCloud->存储空间,
但是要配置一些数据
NSUbiquitousContainers
iCloud.com.example.apple-samplecode.SimpleiCloudDocument
NSUbiquitousContainerIsDocumentScopePublic // 是否支持在File app中查看
NSUbiquitousContainerName // 在File app中显示的名称
SimpleiCloudDocument
NSUbiquitousContainerSupportedFolderLevels
ANY
要实现在File app查看自己app中数据,并实现点击自定义格式文件能打开app,要实现配置CFBundleDocumentTypes and UTExportedTypeDeclarations
设置LSSupportsOpeningDocumentsInPlace = true
实现scene(_:openURLContexts:) 方法 具体配置看demo
UTExportedTypeDeclarations
UTTypeConformsTo
com.apple.package
public.content
public.data
UTTypeDescription
AIScanner
UTTypeIconFiles
Icon.png
UTTypeIdentifier
com.daping.pdfdoc.icloud.aisc
UTTypeTagSpecification
public.filename-extension
aisc
public.mime-type
application/aisc
CFBundleDocumentTypes
CFBundleTypeIconFiles
CFBundleTypeName
AIScanner
CFBundleTypeRole
Editor
LSHandlerRank
Owner
LSTypeIsPackage
LSItemContentTypes
com.daping.pdfdoc.icloud.aisc
NSUbiquitousContainers
iCloud.com.danping.pdfdoc.aiscanner
NSUbiquitousContainerIsDocumentScopePublic
NSUbiquitousContainerName
SimpleiCloudDocument
NSUbiquitousContainerSupportedFolderLevels
ANY
LSSupportsOpeningDocumentsInPlace
3. 查询数据
如何查询icloud document
当在一个设备添加了document, icloud并不会立即把数据同步到其他设备,而是先同步metadata,因为metadata 数据量小,根据不同设置选择是否立即同步数据,iOS设备不会立即同步,mac会立即同步。
其他设置收到icloud 新数据通知后,数据并没有同步过来,所有不能使用FileManager API 去操作数据。
要查询和监听icloud 数据变换要使用 NSMetadataQuery 接口。
metadataQuery.notificationBatchingInterval = 1
metadataQuery.searchScopes = [NSMetadataQueryUbiquitousDataScope, NSMetadataQueryUbiquitousDocumentsScope]
metadataQuery.predicate = NSPredicate(format: "%K LIKE %@", NSMetadataItemFSNameKey, "*." + Document.extensionName)
metadataQuery.sortDescriptors = [NSSortDescriptor(key: NSMetadataItemFSNameKey, ascending: true)]
metadataQuery.start()
当读取数据的时候,为了避免冲突,先关闭查询接口,然后再开启。
func metadataItemList() -> [MetadataItem] {
var result = [MetadataItem]()
metadataQuery.disableUpdates()
if let metadatItems = metadataQuery.results as? [NSMetadataItem] {
result = metadataItemList(from: metadatItems)
}
metadataQuery.enableUpdates()
return result
}
4. 冲突解决
icloud 多个设备之前同步数据,不同的网络环境,冲突不可避免。比如多个设备都断网,然后同时修改数据,再同时打开网络, 就会出现冲突。
@objc
func documentStateChanged(_ notification: Notification) {
guard let document = document else { return }
printDocumentState(for: document)
// The document state is normal.
// Update the UI with unpresented peer changes, if any.
//
if document.documentState == .normal {
navigationItem.rightBarButtonItem?.isEnabled = true
handleConflictsItem.isEnabled = false
if !document.unpresentedPeerChanges.isEmpty {
let changes = document.unpresentedPeerChanges
document.clearUnpresentedPeerChanges()
updateCollectionView(with: changes)
}
return
}
// The document has conflicts but no error.
// Update the UI with unpresented peer changes if any.
//
if document.documentState == .inConflict {
navigationItem.rightBarButtonItem?.isEnabled = true
handleConflictsItem.isEnabled = true
if !document.unpresentedPeerChanges.isEmpty {
let changes = document.unpresentedPeerChanges
document.clearUnpresentedPeerChanges()
updateCollectionView(with: changes)
}
return
}
// The document is in a closed state with no error. Clear the UI.
//
if document.documentState == .closed {
navigationItem.rightBarButtonItem?.isEnabled = false
handleConflictsItem.isEnabled = false
title = ""
var snapshot = DiffableImageSourceSnapshot()
snapshot.appendSections([0])
diffableImageSource.apply(snapshot)
return
}
// The document has conflicts. Enable the toolbar item.
//
if document.documentState.contains(.inConflict) {
handleConflictsItem.isEnabled = true
}
// The document is editingDisabled. Disable the UI for editing.
//
if document.documentState.contains(.editingDisabled) {
navigationItem.rightBarButtonItem?.isEnabled = false
handleConflictsItem.isEnabled = false
}
}
上面代码显示 当打开一个document后,可以监听document的变化,包括冲突
不过解决冲突很简单的,Document 监听状态变化,当检测到冲突的时候,根据不同策略去解决,比如再简单的根据修改时间,最新的保留,其他的删除。
private func resolveConflictsAsynchronously(document: Document, completionHandler: ((Bool) -> Void)?) {
DispatchQueue.global().async {
NSFileCoordinator().coordinate(writingItemAt: document.fileURL,
options: .contentIndependentMetadataOnly, error: nil) { newURL in
let shouldRevert = self.pickLatestVersion(for: newURL)
completionHandler?(shouldRevert)
}
}
}
private func pickLatestVersion(for documentURL: URL) -> Bool {
guard let versionsInConflict = NSFileVersion.unresolvedConflictVersionsOfItem(at: documentURL),
let currentVersion = NSFileVersion.currentVersionOfItem(at: documentURL) else {
return false
}
var shouldRevert = false
var winner = currentVersion
for version in versionsInConflict {
if let date1 = version.modificationDate, let date2 = winner.modificationDate,
date1 > date2 {
winner = version
}
}
if winner != currentVersion {
do {
try winner.replaceItem(at: documentURL)
shouldRevert = true
} catch {
print("Failed to replace version: \(error)")
}
}
do {
try NSFileVersion.removeOtherVersionsOfItem(at: documentURL)
} catch {
print("Failed to remove other versions: \(error)")
}
return shouldRevert
}