如何使用 UIDocumentBrowserViewController 在 iCloud 上重新打开 UIDocument
How to reopen a UIDocument on iCloud using UIDocumentBrowserViewController
我使用 UIDocumentBrowserViewController
(它是一个游戏)开发了一个基于文档的 iOS 应用程序。在应用程序启动时,我想自动重新打开上次使用的 UIDocument
。为此,我将创建或打开的 UIDocument
的 url 字符串存储在用户默认值中,并尝试在我的 DocumentBrowserViewController
的 viewDidLoad
上打开它。这对于本地存储上的文档工作正常,但在 iCloud(或其他一些外国)存储的文档上失败。
本地存储文档的 url 字符串如下所示:
/private/var/mobile/Containers/Data/Application/F4AF5E0B-C261-47F5-95ED-0B8A1DFEEDE6/Documents/Emsave.emp
某些云服务上的同一文档有这个 url 字符串:
/private/var/mobile/Containers/Shared/AppGroup/83CB33FC-5A87-404F-BFB9-8F2910A2192E/File
Provider Storage/485172592867551584/Emsave.emp
我的 DocumentBrowserViewController 的 viewDidLoad:
override func viewDidLoad() {
super.viewDidLoad()
delegate = self
allowsDocumentCreation = true
allowsPickingMultipleItems = false
// Do any additional setup after loading the view, typically from a nib.
// did we already open a game before?
if let url = UserDefaults.standard.string(forKey: DocumentBrowserViewController.lastGameKey) {
print("last opened game: \(url)")
if FileManager.default.fileExists(atPath: url) { // and is the game still existing?
presentDocument(at: URL(fileURLWithPath: url)) // open it
}
}
}
测试 FileManager.default.fileExists(atPath: url) 似乎 return 如果 url 字符串指定存储在云服务上的文档,尽管它在那里。
您不应该保留从 DocumentPicker 或 DocumentBrowser 获得的 url,因为文件可能已经移动,在您的应用程序关闭时已重命名。
您应该保存 url 中的书签,并在下次需要时从书签中重新创建 url。
查看此部分:书签和安全范围
在 https://developer.apple.com/documentation/foundation/nsurl?language=objc
我使用 UIDocumentBrowserViewController
(它是一个游戏)开发了一个基于文档的 iOS 应用程序。在应用程序启动时,我想自动重新打开上次使用的 UIDocument
。为此,我将创建或打开的 UIDocument
的 url 字符串存储在用户默认值中,并尝试在我的 DocumentBrowserViewController
的 viewDidLoad
上打开它。这对于本地存储上的文档工作正常,但在 iCloud(或其他一些外国)存储的文档上失败。
本地存储文档的 url 字符串如下所示:
/private/var/mobile/Containers/Data/Application/F4AF5E0B-C261-47F5-95ED-0B8A1DFEEDE6/Documents/Emsave.emp
某些云服务上的同一文档有这个 url 字符串:
/private/var/mobile/Containers/Shared/AppGroup/83CB33FC-5A87-404F-BFB9-8F2910A2192E/File Provider Storage/485172592867551584/Emsave.emp
我的 DocumentBrowserViewController 的 viewDidLoad:
override func viewDidLoad() {
super.viewDidLoad()
delegate = self
allowsDocumentCreation = true
allowsPickingMultipleItems = false
// Do any additional setup after loading the view, typically from a nib.
// did we already open a game before?
if let url = UserDefaults.standard.string(forKey: DocumentBrowserViewController.lastGameKey) {
print("last opened game: \(url)")
if FileManager.default.fileExists(atPath: url) { // and is the game still existing?
presentDocument(at: URL(fileURLWithPath: url)) // open it
}
}
}
测试 FileManager.default.fileExists(atPath: url) 似乎 return 如果 url 字符串指定存储在云服务上的文档,尽管它在那里。
您不应该保留从 DocumentPicker 或 DocumentBrowser 获得的 url,因为文件可能已经移动,在您的应用程序关闭时已重命名。
您应该保存 url 中的书签,并在下次需要时从书签中重新创建 url。
查看此部分:书签和安全范围 在 https://developer.apple.com/documentation/foundation/nsurl?language=objc