【问题标题】:Swift crash wen tapping “Open in Instagram” while using UIDocumentInteractionController使用 UIDocumentInteractionController 时点击“在 Instagram 中打开”导致 Swift 崩溃
【发布时间】:2015-09-23 11:51:53
【问题描述】:

我有以下代码用于从我的 Swift 应用程序在 Instagram 上共享图像: @IBAction func instagramShareButton(sender: AnyObject) {

    let documentsDirectory = NSSearchPathForDirectoriesInDomains(.DocumentDirectory, .UserDomainMask, true)[0] as NSString
    let path = documentsDirectory.stringByAppendingPathComponent("Share Icon.igo")

    let imageName: String = "Share Icon.png"
    let image = UIImage(named: imageName)
    let data = UIImagePNGRepresentation(image!)

    data!.writeToFile(path, atomically: true)

    let imagePath = documentsDirectory.stringByAppendingPathComponent("Share Icon.igo")
    let rect = CGRectMake(0, 0, 0, 0)

    UIGraphicsBeginImageContextWithOptions(self.view.bounds.size, self.view.opaque, 0.0)
    self.view.layer.renderInContext(UIGraphicsGetCurrentContext()!)
    UIGraphicsEndImageContext()

    let fileURL = NSURL(fileURLWithPath: imagePath)
    print("fileURL = \(fileURL)")

    var interactionController = UIDocumentInteractionController(URL: fileURL)

    interactionController.delegate = self

    interactionController.UTI = "com.instagram.exclusivegram"

    let msgBody = "My message"
    interactionController.annotation = NSDictionary(object: msgBody, forKey: "InstagramCaption")
    interactionController.presentOpenInMenuFromRect(rect, inView: self.view, animated: true)
}

func documentInteractionControllerWillPresentOpenInMenu(controller: UIDocumentInteractionController) { }

我将代码从 Objective C 翻译成 Swift,因为我没有在 Swift 中找到任何可以在 Instagram 上分享的内容。

菜单弹出,我在那里看到Instagram,当我点击它时,我收到以下错误:

-[_UIOpenWithAppActivity performActivity] 中的断言失败, /BuildRoot/Library/Caches/com.apple.xbs/Sources/UIKit/UIKit-3505.16/UIDocumentInteractionController.m:408

*** 由于未捕获的异常“NSInternalInconsistencyException”而终止应用程序,原因: 'UIDocumentInteractionController 过早地消失了!'

我相信我必须以某种方式释放 UIDocumentInteractionController 对象。我对吗? 还没有找到任何信息来帮助我理解如何在 Swift 中做到这一点。请帮我弄清楚如何解决这个问题。

【问题讨论】:

  • 这可能是我不久前尝试做同样事情时遇到的同样问题。我从未找到解决方案,但能够实施令我的应用程序满意的解决方法。你可以在这里看到我的帖子:link

标签: swift share instagram release retain


【解决方案1】:

我认为你是对的。 我也有同样的问题。

在开始分享功能之前,你必须从 UIDocumentInteractionController 创建一个全局变量:

var interactionController: UIDocumentInteractionController?
@IBAction func instagramShareButton(sender: AnyObject) {
    ...
    interactionController = UIDocumentInteractionController(URL: fileURL)
    interactionController!.UTI = "com.instagram.exclusivegram"
    let msgBody = "My message"
    interactionController!.annotation = NSDictionary(object: msgBody, forKey: "InstagramCaption")
    interactionController!.presentOpenInMenuFromRect(rect, inView: self.view, animated: true)
}

这对我有用!

【讨论】:

  • 感谢您的回答。它确实适用于全局 UIDocumentInteractionController 变量。原来在程序结束时,它不再知道interactionController是什么,所以它崩溃了。
  • 发生这种情况是因为当用户在控制器中选择任何启用的应用程序时需要UIDocumentInteractionController 实例。它崩溃是因为当用户点击应用程序时控制器被释放。我们在这里需要全局变量来保持文档控制器的活动。请注意,仅导出到 AirDrop 时,不使用文档控制器,并且可以是局部变量。
  • 全局意味着全局类变量,顺便说一句,正如上面代码中或多或少所示。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2013-08-28
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多