4

This worked fine on iOS 8, but on iOS 9 the UIDocumentInteractionController does appear with the option Copy to Instagram. Pressing it just dismisses the controller.

Any feedback would be appreciated.

Thanks,

    var docController = UIDocumentInteractionController()
    let instagramURL = NSURL(string: "instagram://app")

    if(UIApplication.sharedApplication().canOpenURL(instagramURL!)) {
        var imagePost = cropped
        let fullPath = documentsDirectory().stringByAppendingString("insta.igo")
        var imageData = UIImagePNGRepresentation(imagePost!)!.writeToFile(fullPath, atomically: true)
        let rect = CGRectMake(0, 0, 0, 0)
        self.docController.UTI = "com.instagram.exclusivegram"
        let igImageHookFile = NSURL(string: "file://\(fullPath)")
        self.docController = UIDocumentInteractionController(URL: igImageHookFile!)

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

    }

    func documentsDirectory() -> String {
    let documentsFolderPath = NSSearchPathForDirectoriesInDomains(NSSearchPathDirectory.DocumentDirectory, NSSearchPathDomainMask.UserDomainMask, true)[0]
    return documentsFolderPath
}
carol
  • 71
  • 5

2 Answers2

3

The way iOS9 reads it when declaring "insta.igo", now needs to have the "/"

let fullPath = documentsDirectory().stringByAppendingString("/insta.igo")

Complete code

var docController = UIDocumentInteractionController()
let instagramURL = NSURL(string: "instagram://app")

if(UIApplication.sharedApplication().canOpenURL(instagramURL!)) {
    var imagePost = cropped
    let fullPath = documentsDirectory().stringByAppendingString("/insta.igo")
    var imageData = UIImagePNGRepresentation(imagePost!)!.writeToFile(fullPath, atomically: true)
    let rect = CGRectMake(0, 0, 0, 0)
    self.docController.UTI = "com.instagram.exclusivegram"
    let igImageHookFile = NSURL(string: "file://\(fullPath)")
    self.docController = UIDocumentInteractionController(URL: igImageHookFile!)

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

}

func documentsDirectory() -> String {
let documentsFolderPath = NSSearchPathForDirectoriesInDomains(NSSearchPathDirectory.DocumentDirectory, NSSearchPathDomainMask.UserDomainMask, true)[0]
return documentsFolderPath
pkamb
  • 33,281
  • 23
  • 160
  • 191
carol
  • 71
  • 5
  • Sidenote: The `var docController = UIDocumentInteractionController()` **should be global** for the class/VC, don't put it inside `func`. Try it @noobsmcgoobs. – Dan Apr 13 '16 at 09:58
  • 1
    @John I cannot thank you enough, your comment should be an answer in its own right, and I'll upvote it. – xytor Jun 14 '18 at 07:10
0

This solution provided by Andy Shephard worked for me: https://stackoverflow.com/a/32616355/1500708

Community
  • 1
  • 1
CodeMonkey
  • 635
  • 1
  • 7
  • 16