• 设为首页
  • 点击收藏
  • 手机版
    手机扫一扫访问
    迪恩网络手机版
  • 关注官方公众号
    微信扫一扫关注
    迪恩网络公众号

ios - UIDocumentInteractionController 不工作

[复制链接]
菜鸟教程小白 发表于 2022-12-12 13:26:04 | 显示全部楼层 |阅读模式 打印 上一主题 下一主题

在我的应用程序中,我想下载各种类型的文件并导出,以便用户可以使用打开此类文件的应用程序打开文件。我正在尝试使用 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?["rodutoBiblioteca"] 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
    }



Best Answer-推荐答案


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

我已经查看了您的代码,我认为问题出在下面一行

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

您正在 block 内创建 UIDocumentInteractionController 对象,因此当 block 内存不足或被释放时,您的 UIDocumentInteractionController 对象也将被释放。

所以让 UIDocumentInteractionController 对象成为全局对象。为该类全局声明对象,然后在 block 内赋值。

关于ios - UIDocumentInteractionController 不工作,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/43848833/

回复

使用道具 举报

懒得打字嘛,点击右侧快捷回复 【右侧内容,后台自定义】
您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

关注0

粉丝2

帖子830918

发布主题
阅读排行 更多
广告位

扫描微信二维码

查看手机版网站

随时了解更新最新资讯

139-2527-9053

在线客服(服务时间 9:00~18:00)

在线QQ客服
地址:深圳市南山区西丽大学城创智工业园
电邮:jeky_zhao#qq.com
移动电话:139-2527-9053

Powered by 互联科技 X3.4© 2001-2213 极客世界.|Sitemap