在使用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)
}

这对我有

相关文章

软件简介:蓝湖辅助工具,减少移动端开发中控件属性的复制和粘...
现实生活中,我们听到的声音都是时间连续的,我们称为这种信...
前言最近在B站上看到一个漂亮的仙女姐姐跳舞视频,循环看了亿...
【Android App】实战项目之仿抖音的短视频分享App(附源码和...
前言这一篇博客应该是我花时间最多的一次了,从2022年1月底至...
因为我既对接过session、cookie,也对接过JWT,今年因为工作...