UIDocumentInteractionController无法正常工作

在我的应用程序中,我想下载各种类型的文件和导出,以便用户可以使用打开此类文件的应用程序打开该文件。 我正在尝试使用UIDocumentInteractionController,但在下载后将其保存到我的设备并尝试打开它时会发生几个问题。 使用.docx,应用程序会中断,而对于其他文件,它似乎不会对文件进行编码。 我能做错什么?

当我尝试通过空投共享文件时,我收到一条错误消息,指出该文件无效。

当我与iBooks分享时,我收到消息”NSInternalInconsistencyException’,原因是:’UIDocumentInteractionController过早地消失了! “应用程序中断了

这是我的代码:

func loadArchiveAtIndex(sender: NSNotification){ let itemIndex = sender.userInfo!["index"] as! Int let archiveKey = sender.userInfo!["downloadKey"] as! String self.downloadingItens.append(itemIndex) SQLiteDataController().getTokenSincronismo({ (Valido, Retorno, Token, Tipo) -> Void in if Valido == true{ switch Retorno { case 9: // Retorno Válido let params = [ "tokenSincronizacao":"\(Token)", "chaveProdutoBiblioteca":"\(archiveKey)" ] Alamofire.request(.GET, "\(_URLPREFIX)/WSMhobErpServico/api/sincronizar/ProdutoBiblioteca/ObtemProdutoBiblioteca", parameters: params).responseJSON { (response) in if response.result.isFailure { print(response.result.error) } else { let result = response.result.value if let archive = result?["ProdutoBiblioteca"] as? NSDictionary{ let bytes = archive.objectForKey("Arquivo") as! String let name = archive.objectForKey("NomeArquivo") as! String let data = NSData.init(base64EncodedString: bytes, options: .IgnoreUnknownCharacters) let url = NSURL.init(string: name.lowercaseString) data?.writeToURL(url!, atomically: true) let documents = try! NSFileManager.defaultManager().URLForDirectory(.DocumentDirectory, inDomain: .UserDomainMask, appropriateForURL: nil, create: false) let fileURL = documents.URLByAppendingPathComponent((name.lowercaseString)) let dic = UIDocumentInteractionController.init(URL: fileURL!) dic.delegate = self dic.presentOpenInMenuFromRect(CGRect.init(x: 0, y: 0, width: 200, height: 200), inView: self.view, animated: true) var arrayIndex = 0 for item in self.downloadingItens { let i = item if i == itemIndex { self.downloadingItens.removeAtIndex(arrayIndex) NSNotificationCenter.defaultCenter().postNotificationName("reloadTable", object: nil, userInfo: ["itens":self.downloadingItens]) break } arrayIndex = arrayIndex + 1 } } else { print("erro") } } } break default: self.showError("Atenção", message: "Não foi Possivel Processar a sua Solicitação") break } }else{ self.showError("Atenção", message: "Não foi Possivel Processar a sua Solicitação") } }, sincFull:true) } func documentInteractionControllerViewForPreview(controller: UIDocumentInteractionController) -> UIView? { return self.view } func documentInteractionControllerRectForPreview(controller: UIDocumentInteractionController) -> CGRect { return self.view.frame } func documentInteractionControllerViewControllerForPreview(controller: UIDocumentInteractionController) -> UIViewController { return self } 

好的,我明白了你的意思。

我已经审核了您的代码,我认为问题出在下面

  let dic = UIDocumentInteractionController.init(URL: fileURL!) dic.delegate = self 

您正在块内创建UIDocumentInteractionController的对象,因此当块将缺少内存或取消分配时,您的UIDocumentInteractionController对象也将被释放。

因此,使UIDocumentInteractionController对象全局化。 为该类声明全局对象,然后在块内分配值。

 let name = archive.objectForKey("NomeArquivo") as! String let data = NSData.init(base64EncodedString: bytes, options: .IgnoreUnknownCharacters) let documents = try! NSFileManager.defaultManager().URLForDirectory(.DocumentDirectory, inDomain: .UserDomainMask, appropriateForURL: nil, create: false) let fileURL = documents.URLByAppendingPathComponent((name.lowercaseString)) data?.writeToURL(fileURL!, atomically: true)