开发者

Opening in app, getting UIApplicationLaunchOptionsURLKey from launchOptions

开发者 https://www.devze.com 2023-03-13 20:17 出处:网络
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'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 *)[laun开发者_如何学CchOptions 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?


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).


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]]]


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


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.

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号