麻烦让UIDocumentBrowserController在基于文档的应用程序中打开文档

Ste*_*ter 5 cocoa-touch document-based uidocument uidocumentbrowservc

我一直在研究一个新的基于文档的应用程序,并且非常高兴新的UIDocumentBrowserController ...试图为文档浏览器推出我自己的解决方案UI很棘手!

我在浏览器创建后打开文档时遇到了一些麻烦.

现在发生的是,当我选择在文档浏览器中创建新文档时,会按预期创建和打开文档,但会记录错误消息.但是,在关闭文档后,即使显示文档,也无法立即或在后续启动时重新打开文件.但是,一个奇怪的线索是,如果我在创建文档后停止运行应用程序,但没有向其添加新信息(触发保存周期),并再次运行项目,我可以正确打开文件.这让我觉得文件被保存的方式就是问题所在.(注意:在此阶段,我正在努力使本地,非/ icloud实现工作,然后我继续进行icloud实现.)

以下是文档保存到磁盘时代码中任何一点的错误消息(或至少大部分时间!):2017-06-20 13:21:58.254938-0500布道设计2 iOS [22454:5000138] [ default] [ERROR]无法获取项目文件的属性值:/// Users/stevenhovater/Library/Developer/CoreSimulator/Devices/9A4364F2-B3A1-4AD9-B680-FB4BC876C707/data/Containers/Data/Application/DD534ED8-C4A3 -40FE-9777-AED961976878 /文档/无标题-9.sermon.错误:错误域= NSFileProviderInternalErrorDomain代码= 1"不允许读者访问该URL." UserInfo = {NSLocalizedDescription =不允许读者访问该URL.}

我怀疑问题在于我的文档类型plists,我试图通过在wwdc 2017 session 229中模仿视频中的设置来设置.

在此输入图像描述 我的文档被一个NSData对象所包围,使用我所采用的UIDocument非常标准的子类实现.(我省略了生成缩略图的代码)

override func contents(forType typeName: String) throws -> Any {  

    print("Saving Document Changes")  
    if sermon != nil {  
        let newData = NSKeyedArchiver.archivedData(withRootObject: sermon!)  

        return newData  
    } else {   
        let newData = NSKeyedArchiver.archivedData(withRootObject: Sermon())  

        return newData       
    }  
}  

override func fileAttributesToWrite(to url: URL, for saveOperation: UIDocumentSaveOperation) throws -> [AnyHashable : Any] {  

    let thumbnail:UIImage = self.createThumbnail()       

    let thumbnaildict = [URLThumbnailDictionaryItem.NSThumbnail1024x1024SizeKey : thumbnail]  
    let dict = [URLResourceKey.thumbnailDictionaryKey:thumbnaildict]  
   return dict        
}  

override func load(fromContents contents: Any, ofType typeName: String?) throws {  

    guard let newSermon:Sermon = NSKeyedUnarchiver.unarchiveObject(with: contents as! Data) as? Sermon else{  
        throw documentErrors.invalidFile  
    }  

    self.sermon = newSermon  

}  
Run Code Online (Sandbox Code Playgroud)

在我的UIDocumentBrowserViewController的子类中,这是我获取本地文件名和创建新文档的代码.

func documentBrowser(_ controller: UIDocumentBrowserViewController, didRequestDocumentCreationWithHandler importHandler: @escaping (URL?, UIDocumentBrowserViewController.ImportMode) -> Void) {  
    var newDocumentURL: URL? = nil  

        print("creating new local document")  

        guard let target  = self.newLocalFilename() else {  
            return  
        }  
        let targetSuffix = target.lastPathComponent  
        let tempURL = URL(fileURLWithPath: NSTemporaryDirectory() + targetSuffix)  

        let newDocument:SDDocument = SDDocument(fileURL: tempURL)  

        newDocument.sermon = Sermon()  

        /  
        newDocument.save(to: tempURL, for: .forCreating) { (saveSuccess) in  

            /  
            guard saveSuccess else {  
                /  
                importHandler(nil, .none)  
                return  
            }  

            /  
            newDocument.close(completionHandler: { (closeSuccess) in  

                /  
                guard closeSuccess else {  
                    /  
                    importHandler(nil, .none)  
                    return  
                }  

                /  
                importHandler(tempURL, .move)  
            })  
        }  

}  

func newLocalFilename() -> URL? {  
    let fileManager = FileManager()  

    guard let baseURL = self.localDocumentsDirectoryURL.appendingPathComponent("Untitled")  

        else {return nil}  

    var target = baseURL.appendingPathExtension(DocumentBrowserViewController.documentExtension)  

    var nameSuffix = 2  

    while fileManager.fileExists(atPath: target.path) {  
        target = URL(fileURLWithPath: baseURL.path + "-\(nameSuffix).\(DocumentBrowserViewController.documentExtension)")  

        nameSuffix += 1  
    }  
    let targetSuffix = target.lastPathComponent  
    print("Target name: \(targetSuffix)")  
    print("new url: \(target)")  

    return target  

}  
Run Code Online (Sandbox Code Playgroud)

mat*_*att 3

经过四五个小时的努力,我发现了一个简单的解决方案:不要在 Simulator 中进行测试。我转而在我的设备上进行测试,一切立即开始按照广告宣传的那样工作。

[我不能根据这里的经验来说话,但可能“在模拟器中不起作用”问题仅限于 Sierra,但模拟器在 High Sierra 中确实有效。这可以解释为什么有些用户看到这个问题而其他用户却看不到,尤其是为什么苹果在 WWDC 视频中似乎幸福地没有意识到这个问题。]