15

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.

asheyla
  • 3,175
  • 5
  • 18
  • 34
  • this may be the same issue I faced a while back trying to do the same thing. I never found a solution, but was able to implement a workaround that satisfied my App. You can see my post here: [link](http://stackoverflow.com/questions/29065772/ios-uidocumentinteractioncontroller-launchservices-invalidationhandler-called) – Lavvo Sep 23 '15 at 15:31

1 Answers1

43

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!

feca
  • 1,119
  • 16
  • 14
  • Thank you for your answer. It really worked with the global UIDocumentInteractionController variable. Turns out that at the end of the program, it no longer knows what interactionController is, so it crashes. – asheyla Sep 24 '15 at 07:54
  • 2
    This happens because `UIDocumentInteractionController` instance is needed when user selects any enabled app in the controller. It crashes because controller is released when user taps on an app. We need global variable here to keep the document controller alive. Note when exporting just to AirDrop, document controller is not used and can be local variable. – Boris Y. Jan 24 '16 at 10:14
  • 2
    With global is meant a global class variable, btw, as more or less shown in code above. – Olivier de Jonge Mar 07 '16 at 10:23