Swift 使用 UIDocumentInteractionController 时点击“在 Instagram 中打开”会崩溃
Swift crash wen tapping “Open in Instagram” while using UIDocumentInteractionController
我有以下代码用于从我的 Swift 应用程序在 Instagram 上分享图片:
@IBAction func instagramShareButton(发件人: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(控制器: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, inView: self.view, animated: true)
}
这对我有用!
我有以下代码用于从我的 Swift 应用程序在 Instagram 上分享图片: @IBAction func instagramShareButton(发件人: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(控制器: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, inView: self.view, animated: true)
}
这对我有用!