Reputation: 567
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")
}
}
}
}
}
Upvotes: 2
Views: 1471
Reputation: 11096
I had the same problem on an iPad which was pretty much right out of the box. I couldn't open txt or png files (the two tests I ran). presentOpenInMenuFromRect
always returned NO
.
I found that the accepted answer of using presentOptionsMenuFromRect
did launch that window, but it wasn't precisely what I wanted. (It could be what some of you will want though).
It turned out that my device simply didn't have an app associated with either of these types by default! (How ridiculous is that?) I assumed they were so common that would never be an issue.
So, I randomly installed the first free file manager app that I found in the app store (USB Disk Free was what I picked). Then, my device gave me the option to open either of those types with that app.
Upvotes: 0
Reputation: 567
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.
Upvotes: 3
Reputation: 57060
The problem is you are writing your file asynchronously on a background queue, but do not wait before opening your interaction controller. So when it attempts to open your file, it cannot find it because it hasn't been written just yet. You need to let the write operation succeed, then attempt to open the interaction controller.
Change your code like so:
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) {
dispatch_async(dispatch_get_main_queue(), { () -> Void in
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")
}
}
}
}
}
})
Upvotes: 0