【问题标题】:How to reopen a UIDocument on iCloud using UIDocumentBrowserViewController如何使用 UIDocumentBrowserViewController 在 iCloud 上重新打开 UIDocument
【发布时间】:2019-10-12 21:50:37
【问题描述】:

我使用UIDocumentBrowserViewController(它是一个游戏)开发了一个基于文档的 iOS 应用程序。在应用程序启动时,我想自动重新打开上次使用的UIDocument。为此,我将创建或打开的UIDocument 的url 字符串存储在用户默认值中,并尝试在我的DocumentBrowserViewControllerviewDidLoad 上打开它。这适用于本地存储上的文档,但在 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


    【解决方案1】:

    您不应保留从 DocumentPicker 或 DocumentBrowser 获得的网址,因为文件可能已移动,在您的应用关闭时已重命名。

    您应该从 url 中保存一个书签,并在下次需要时从该书签中重新创建 url。

    看看这个部分:书签和安全范围 在https://developer.apple.com/documentation/foundation/nsurl?language=objc

    【讨论】:

    • 非常感谢您的提示,但它并没有真正解决问题。如果 URL 指向外部存储(如 iCloud),我无法创建书签。在 iOS 上,BookMarkCreationOption withSecurityScope 不可用,如本讨论所述 [stackoverflow.com/questions/52524827/… 和权利 com.apple.security.files.bookmarks.app-scope 似乎在 iOS 上无效。
    • 您是否尝试过在 [url startAccessingSecurityScopedResource] 和 [url stopAccessingSecurityScopedResourde] 之间包含书签创建?
    • 是的,没有那个。非常感谢;现在一切都很好。
    猜你喜欢
    • 2020-04-15
    • 2018-03-13
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2014-11-18
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多