我有以下代码在我的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 / Cache / com.apple.xbs / Source / UIKit / UIKit-3505.16 / UIDocumentInteractionController.m:408 ***由于未捕获的异常'NSInternalInconsistencyException'而终止应用程序,原因:'UIDocumentInteractionController过早地消失了!'
我相信我不得不释放UIDocumentInteractionController对象 . 我对吗?没有找到任何信息来帮助我理解我如何在Swift中做到这一点 . 请帮我弄清楚如何解决这个问题 .
1 回答
我认为你是对的 . 我也有同样的问题 .
在开始共享功能之前,必须从UIDocumentInteractionController创建一个全局变量:
这适合我!