1

I'm having an issue when I try to Launch Instagram from my app. Everything works and I'm able to launch IG and even see the photo I sent etc.

The problem is that the UIDocumentInteractionController is crashing my app. And YES I have done my research.

I've seen the posts LIKE THIS that indicate that this is an Apple bug as as long as you can fix the crash, you should be able fine and ignore the Launch Services message.

The problem is I am still having the crash and trying to figure out how to resolve it.

I found a post that talks about adding an IF-STATEMENT after presenting the ViewController HERE, this post was written in Objective-C, and the example was not for a UIDocumentInteractionController.

I tried to take a stab at it in Swift, but it is still not working out for me. Would appreciate if someone can help out.

dic = UIDocumentInteractionController(URL: imageURL!)
dic.delegate = self

var annotationDictionary: [String: String] = ["InstagramCaption": "Test"]
dic.annotation = annotationDictionary
dic.UTI = "com.instagram.exclusivegram"

dic.presentOpenInMenuFromRect(CGRectMake(1, 1, 1, 1), inView: self.view, animated: true)

if dic.respondsToSelector("popoverPresentationController") {

    Scripts.log("InstagramVC Did Respond to popoverPresentationController")     
    var popoverController: UIPopoverPresentationController = self.popoverPresentationController!        
    popoverPresentationController?.sourceView = self.view       
}
Community
  • 1
  • 1
Lavvo
  • 1,024
  • 3
  • 16
  • 35

3 Answers3

3

The fix in my case was to declare the UIDocumentInteractionController variable as part of the viewcontroller's class instead of creating it in the same function where I set up the annotation and UTI and called .presentOpenInMenuFromRect

So near the top of my class outside of any functions I declared the variable:

var docController = UIDocumentInteractionController()

And then when I was ready to use it, I configured everything about the already existing UIDocumentInteractionController instead of creating one:

docController = UIDocumentInteractionController(URL: imageURL!)
docController.UTI = "com.instagram.exclusivegram"
docController.delegate = self
docController.annotation = ["InstagramCaption":"Text"]

docController.presentOpenInMenuFromRect(rect, inView: self.view, animated: true)

The app stopped crashing and Instagram now loads with the image/text assigned.

I found the suggestion that led me to this fix here: https://stackoverflow.com/a/16057399/428981 and then adapted for Swift

Community
  • 1
  • 1
RanLearns
  • 4,086
  • 5
  • 44
  • 81
  • hmmm...interesting. I may have to give this one more try sometime. I kinda did this during my frustration, but looking at yours in more detail, mine was slightly different. I tried declaring on top of my class as well, but I instantiated it inside the function. I can see what you did was declare and instantiate it on top of the class itself. It may seem like a silly little difference, but I wonder if that difference will make it to stop crashing. Thx so much for responding. I was thinking to myself I can't be the only one facing this. – Lavvo Mar 19 '15 at 16:00
  • I tried for 2 weeks now bringing with DocumentInteractionController PDF files to Mail or to Outlook. I always declared and initiated the controller locally in a function. The app didn't crash but the PDF did not arrive and I didn't get any error. With declaring the DocumentInteractionController globally it works. Hurray! – C. Ecker Jul 15 '20 at 22:57
1

It sounds like your instance of UIDocumentInteractionController is going out of scope. Try making it a property on the class or some other way to retain it.

EPage_Ed
  • 1,173
  • 11
  • 11
0

I had the same problem: sending the picture to Instagram worked but instantaneously crashed my app. I think it has something to do with how the UIDocumentInteractionController is handled by the system once another app opens. If you try to send the picture via the embedded Facebook or Twitter frameworks that open a popover on top of your app, then no crash happens...

In any case the way I finally made it work is by NOT declaring my viewController as delegate, so:

// dic.delegate = self

The downside being that you can't use any of the delegate methods. In my case I wasn't using them anyway.

Erken
  • 1,448
  • 16
  • 23
  • Thank you for responding. This has been a pain and was the only thing stopping my app, which is actually the most important part. Funny enough, out of frustration, I did the same thing about an hour ago and commented out the delegate...I was so happy it wasn't crashing anymore. But like you said this means I have no access to the delegate methods. In my case, I do need the delegate for knowing when the IG app has been opened, but my workaround for this was to use a combination of the AppDelegate applicationDidEnterBackground function and an NSNotification function to trigger what i needed. – Lavvo Mar 16 '15 at 20:06