【发布时间】:2019-10-12 21:50:37
【问题描述】:
我使用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/文件 提供者存储/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
}
}
}
如果 url 字符串指定了存储在云服务上的文档,那么测试 FileManager.default.fileExists(atPath: url) 似乎总是返回 false。
【问题讨论】:
标签: ios swift url uidocumentbrowservc