Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Swift crash wen tapping “Open in Instagram” while using UIDocumentInteractionController

I have the below code for sharing an image on instagram from my Swift app: @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) { }

The code is translated from Objective C to Swift by me, as I haven't found anything in Swift for sharing on Instagram.

The menu pops up, I see instagram there and when I tap it, I get the below error:

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!'

I believe I somehow have to release the UIDocumentInteractionController object. Am I right? Haven't found any information to help me understand how I can do this in Swift. Please help me figure out how I could solve this out.

like image 411
asheyla Avatar asked Sep 23 '15 11:09

asheyla


1 Answers

I think your are right. I have the same problem.

before begin the share function, you must create a global variable from 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)
}

This works for me!

like image 198
feca Avatar answered Nov 15 '22 07:11

feca