【问题标题】:UIDocumentInteractionController always failsUIDocumentInteractionController 总是失败
【发布时间】:2015-05-08 21:49:11
【问题描述】:

我正在尝试使用UIDocumentInteractionController。我已经检查了我能想到的所有内容,并且所有内容似乎都包含了所有需要的数据。

当调用presentOpenInMenuFromRect(inView:, animated:) 时,返回的 Bool 始终为 false。我的理解是,因为没有任何应用程序支持 UTI。我试过使用扁平的PNG,但仍然没有。有趣的是为什么它不能在我的 iPhone 上运行,它有很多支持通过UIDocumentInteractionController 加载图像的应用程序,但这里是踢球者。在我将项目升级到 Swift 1.2 之前,这段代码运行良好。

我现在忽略了什么吗?我检查了文档,找不到任何我丢失的东西。

注意:我已经在设备和模拟器中对此进行了测试,两者都返回相同的结果。

let image = UIImage(named: "screenshot")

        if let paths = NSSearchPathForDirectoriesInDomains(.DocumentDirectory, .UserDomainMask, true) {
            if paths.count > 0 {
                if let dirPath = paths[0] as? String {
                    let path = dirPath.stringByAppendingPathComponent("screenshot.ig") // igo


                    dispatch_async(dispatch_get_global_queue(QOS_CLASS_BACKGROUND, 0), { () -> Void in
                        if let image = image {
                            NSFileManager.defaultManager().removeItemAtPath(path, error: nil)
                            if UIImagePNGRepresentation(image).writeToFile(path, atomically: true) {

                            }
                        }
                    })

                    if let url = NSURL(fileURLWithPath: path) {
                        let documentController = UIDocumentInteractionController(URL: url)
                        documentController.UTI = "com.instagram.photo" // com.instagram.exclusivegram
                        documentController.annotation = ["InstagramCaption": ""]
                        if !documentController.presentOpenInMenuFromRect(sender.bounds, inView: self.view, animated: true) {
                            println("Can't do it")
                        }
                    }
                }
            }
        }

【问题讨论】:

    标签: ios swift uidocumentinteraction


    【解决方案1】:

    我决定使用presentOptionsMenuFromRect(:, inView:, animated:),它可以完成我想要完成的工作。不知道为什么presentOpenInMenuFromRect(: inView:, animated:) 决定崩溃,但我确实设法让这一切正常工作。

    【讨论】:

      【解决方案2】:

      问题是您在后台队列中异步写入文件,但在打开交互控制器之前不要等待。所以当它试图打开你的文件时,它找不到它,因为它还没有被写入。您需要让写操作成功,然后尝试打开交互控制器。

      像这样更改您的代码:

      dispatch_async(dispatch_get_global_queue(QOS_CLASS_BACKGROUND, 0), { () -> Void in
          if let image = image {
              NSFileManager.defaultManager().removeItemAtPath(path, error: nil)
              if UIImagePNGRepresentation(image).writeToFile(path, atomically: true) {
                  dispatch_async(dispatch_get_main_queue(), { () -> Void in
                      if let url = NSURL(fileURLWithPath: path) {
                          let documentController = UIDocumentInteractionController(URL: url)
                          documentController.UTI = "com.instagram.photo" // com.instagram.exclusivegram
                          documentController.annotation = ["InstagramCaption": ""]
                          if !documentController.presentOpenInMenuFromRect(sender.bounds, inView: self.view, animated: true) {
                              println("Can't do it")
                          }
                      }
                  }
              }
          }
      })
      

      【讨论】:

      • 我已经尝试过了,并且只是在主线程上执行了所有操作。不过我会再试一次。
      • 我可以确认扔在主线程上仍然不能解决问题。仍然返回“Can't do it”控制台日志。
      【解决方案3】:

      我在开箱即用的 iPad 上遇到了同样的问题。我无法打开 txt 或 png 文件(我运行的两个测试)。 presentOpenInMenuFromRect 总是返回 NO。

      我发现使用presentOptionsMenuFromRect 的公认答案确实启动了该窗口,但这并不是我想要的。 (不过,这可能是你们中的一些人想要的)。

      事实证明,我的设备在默认情况下根本没有与这两种类型关联的应用! (这有多荒谬?)我以为它们很常见,永远不会成为问题。

      所以,我随机安装了我在应用商店中找到的第一个免费文件管理器应用(我选择了 USB Disk Free)。然后,我的设备让我可以选择使用该应用打开这些类型中的任何一种。

      【讨论】:

        猜你喜欢
        • 2016-05-06
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多