完整的错误消息是:
[main]***FPBookmarkableStringFromDocumentURL错误,file:///private/var/mobile/Library/Mobile%20Documents/com~apple~CloudDocs/MY_APP/hello.txt->错误域=NSCocoaErrorDomain代码=257"无法打开该文件,因为您没有查看权限。">
当我重新启动我的应用程序&尝试重新打开以前打开的文件(例如,当应用程序"后台"和内存释放时,所以我想重新加载场景的状态(。我遵循了WWDC会议的步骤https://developer.apple.com/videos/play/wwdc2019/212&下载的示例源代码https://developer.apple.com/documentation/uikit/app_and_environment/scenes/supporting_multiple_windows_on_ipad
我基本上复制了他们的配置(window:UIWindow?,带有activity:NSUserActivity(->Bool函数,并对其进行了转换以满足我的需求。它似乎工作得很完美,但我犯了错误。
我使用的是默认的NSUserActivity技术,如https://developer.apple.com/library/archive/documentation/UserExperience/Conceptual/Handoff/AdoptingHandoff/AdoptingHandoff.html#//apple_ref/doc/uid/TP40014338-CH2-SW17
以下是配置功能的主体:
if activity.activityType == "com.myName.MyApp.openedDocumentUserActivity" {
if let documentURL = activity.userInfo?[UIDocument.userActivityURLKey] as? URL {
if let docBrowserVC = window?.rootViewController as? DocumentBrowserViewController {
documentURL.startAccessingSecurityScopedResource() // tried this, it returns false
docBrowserVC.presentDocument(at: documentURL)
return true
}
}
}
return false
如果有人知道变通办法,谢谢。
我必须从URL创建书签数据,并将其保存在NSUserActivity中。使用UIDocument.dococumentURL是不够的,因为它是安全范围的。
用于创建书签(在这个片段中没有错误检查(:
let userActivity = NSUserActivity(activityType: "com.foobar")
let bookmarkData = try? documentURL.bookmarkData()
userActivity.addUserInfoEntries(from: ["URLBookmarkData": bookmarkData])
scene.userActivity = userActivity
然后阅读:
let bookmarkData = userActivity.userInfo?["URLBookmarkData"] as? Data {
let resolvedURL = try? URL(resolvingBookmarkData: bookmarkData, options: NSURL.BookmarkResolutionOptions(), relativeTo: nil, bookmarkDataIsStale: &bookmarkDataIsStale)