Joetjah
Joetjah

Reputation: 6132

Opening in app, getting UIApplicationLaunchOptionsURLKey from launchOptions

I'm trying to open a .pdf-file in my app. I adapted the Info.plist so a .pdf can be opened in my application.

I use the following code:

- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{
    self.window.rootViewController = self.viewController;
    [self.window makeKeyAndVisible];
    thePDFurl = (NSURL *)[launchOptions valueForKey:UIApplicationLaunchOptionsURLKey];
    return YES;
}

In an other class, where my appDelegate (containing that didFinishLaunchingWithOptions), I've got the line:

appDel = [[UIApplication sharedApplication]delegate];
[theLabel setText:[NSString stringWithFormat:@"%@", appDel.thePDFurl]];

Somehow, theLabel always shows (null). What am I missing?

Upvotes: 0

Views: 7336

Answers (4)

XMLSDK
XMLSDK

Reputation: 259

When you are calling within application:didFinishLaunchingWithOptions: ,

self.window.rootViewController = self.viewController;
[self.window makeKeyAndVisible];

[viewController view] was implicitly called and there was no appDel.thePDFurl assigned yet.

Upvotes: 0

Joetjah
Joetjah

Reputation: 6132

I guess you were all right. It just so happened the view got loaded before the method applicationDidFinishLaunching was finished. Thanks all...

Upvotes: 0

Kalle
Kalle

Reputation: 13346

I may be misunderstanding what you're trying to do. If so, ignore.

If you want the user to be able to "Open with..." a PDF using your app, you can implement

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

E.g.

- (BOOL)application:(UIApplication *)application openURL:(NSURL *)url sourceApplication:(NSString *)sourceApplication annotation:(id)annotation
{
    NSLog(@"Open URL:\t%@\n"
     "From source:\t%@\n"
     "With annotation:%@",
     url, sourceApplication, annotation);

    NSString *filepath = [url path];
    //...
    return YES;
}

I'm pretty sure this works for both launching the app and calling it (i.e. if it's already in the background).

Upvotes: 8

Praveen S
Praveen S

Reputation: 10393

You may retain the pdfurl variable and also get the absolute string value from NSURL using the absoluteString method.

[theLabel setText:[NSString stringWithFormat:@"%@", [appDel.thePDFurl absoluteString]]]

Upvotes: 2

Related Questions