Search code examples
iosswiftuidocumentinteraction

UIDocumentInteractionController always fails


I'm trying to use the UIDocumentInteractionController. I've checked everything I can think off and everything appears to have all the data that is needed.

When calling presentOpenInMenuFromRect(inView:, animated:) the Bool that is returned is always false. My understanding is that its because there aren't any apps that support the UTI. I've tried using a flat out PNG and still nothing. Whats interesting is why it wouldn't work on my iPhone which has plenty of apps that support loading images via the UIDocumentInteractionController, but here is the kicker. This code was working fine before I upgraded the project to Swift 1.2.

Am I overlooking something now? I've checked the docs and couldn't find anything that I was missing.

Note: I have tested this on a device and in the simulator and both return the same.

let image = UIImage(named: "screenshot")

        if let paths = NSSearchPathForDirectoriesInDomains(.DocumentDirectory, .UserDomainMask, true) {
            if paths.count > 0 {
                if let dirPath = paths[0] as? String {
                    let path = dirPath.stringByAppendingPathComponent("screenshot.ig") // igo


                    dispatch_async(dispatch_get_global_queue(QOS_CLASS_BACKGROUND, 0), { () -> Void in
                        if let image = image {
                            NSFileManager.defaultManager().removeItemAtPath(path, error: nil)
                            if UIImagePNGRepresentation(image).writeToFile(path, atomically: true) {

                            }
                        }
                    })

                    if let url = NSURL(fileURLWithPath: path) {
                        let documentController = UIDocumentInteractionController(URL: url)
                        documentController.UTI = "com.instagram.photo" // com.instagram.exclusivegram
                        documentController.annotation = ["InstagramCaption": ""]
                        if !documentController.presentOpenInMenuFromRect(sender.bounds, inView: self.view, animated: true) {
                            println("Can't do it")
                        }
                    }
                }
            }
        }

Solution

  • I decided to go with the presentOptionsMenuFromRect(:, inView:, animated:) which does what I am trying to accomplish. No idea why presentOpenInMenuFromRect(: inView:, animated:) decided to break down, but I did mange to get it all working.