"Open with ..." if the application is not already open

My application can read .txt files opened using the "open with ..." dialog. I implemented my reading functions as in

-(BOOL)application:(UIApplication *)application openURL:(NSURL *)url sourceApplication:(NSString *)sourceApplication annotation:(id)annotation 

and

 -(BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions 

methods.

Interestingly, it only works if the application has been opened. If I open the .txt file in ie Mail and select "open with", then my application. I see in the console that

 -(BOOL)application:(UIApplication *)application openURL:(NSURL *)url sourceApplication:(NSString *)sourceApplication annotation:(id)annotation 

called out. I can also see the URL of the correct file, which is supposedly located in

File: //localhost/private/var/mobile/Applications/F15C57D0-4F62-4979-943A-2D387488D59C/Documents/Inbox/myFile.txt

But when I try to open the file, I get a Cocoa error that the files do not exist.

If I repeat the steps (when opening the application in the background) than the file is found, and it works immediately. Why is this?

+4
source share
2 answers

You should only handle case in application:openUrl:sourceApplication:annotations . According to the documentation ( https://developer.apple.com/library/ios/DOCUMENTATION/UIKit/Reference/UIApplicationDelegate_Protocol/Reference/Reference.html#//apple_ref/occ/intfm/UIApplicationDelegate/application:openURL:sourceApplication:annotation:) :

If your application needs to be launched to open the URL, the application first calls the application:willFinishLaunchingWithOptions: and application:didFinishLaunchingWithOptions: , and then this method. The return values โ€‹โ€‹of these methods can be used to prevent this method from being called. (If the application is already running, only this method is called.)

So, there is no longer any need to handle opening files in application:didFinishLaunchingWithOptions:

+4
source

You should work with "Open with .." during application:didFinishLaunchingWithOptions: getting the URL from the options dictionary using the UIApplicationLaunchOptionsURLKey key, and not the url directly passed to application:openUrl:sourceApplication:annotations .

https://developer.apple.com/library/ios/#documentation/FileManagement/Conceptual/DocumentInteraction_TopicsForIOS/Articles/OpeningSupportedFileTypes.html#//apple_ref/doc/uid/TP40010412-SW1

+1
source

All Articles