微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

在使用UIDocumentInteractionController时,迅速捣毁“在Instagram中打开”

我有以下代码,用于从 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)

    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在那里,当我点击它,我得到以下错误

Assertion failure in -[_UIOpenWithAppActivity performActivity],
/buildroot/Library/Caches/com.apple.xbs/Sources/UIKit/UIKit-3505.16/UIDocumentInteractionController.m:408

*** Terminating app due to uncaught exception ‘NSInternalInconsistencyException’,reason:
‘UIDocumentInteractionController has gone away prematurely!’

我相信我不得不释放UIDocumentInteractionController对象.我对吗?
没有找到任何信息来帮助我了解如何在Swift中做到这一点.请帮我找出如何解决这个问题.

我想你是对的
我有同样的问题.

在开始共享功能之前,必须从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,animated: true)
}

这对我有

原文地址:https://www.jb51.cc/swift/319661.html

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。

相关推荐