使用URL启动应用程序,但未调用OpenUrl [英] Launch App using URL, but OpenUrl Not Called

查看:586
本文介绍了使用URL启动应用程序,但未调用OpenUrl的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我已经实现了URL方案,并使用它通过调用方法将数据传递到我的应用程序.整个代码如下所示

I have implemented a URL Scheme and use it to pass data to my app by calling method. The entire code is shown as below

- (BOOL)application:(UIApplication *)application handleOpenURL:(NSURL *)url{
    // Check the calling application Bundle ID
    if ([[url scheme] isEqualToString:@"yuvitime"])
    {
        NSLog(@"URL scheme:%@", [url scheme]);
        NSString * yuvitimeRequestValue = [url query];
        NSDictionary * userInfor = [[NSDictionary alloc]initWithObjectsAndKeys:yuvitimeRequestValue, @"YuvitimeRequest", nil];
        NSNotificationCenter * notificationCentre = [NSNotificationCenter defaultCenter];
        [notificationCentre postNotificationName:@"URLSCHEMEACTIVATEDNOTIFICATION" object:self userInfo:userInfor];

        return YES;
    }
    else
        return NO;
}

如果我的应用程序在后台运行,则一切正常.当您单击一个URL时,该应用程序将返回到Foreground,并且该URL将按照上述函数中的代码进行处理.

If my app is in the background, everything works fine. When you click a URL, the app is brought back to Foreground and the URL is handled as coded in the above function.

但是,如果终止了该应用程序(尚未启动应用程序),则通过单击URL只会启动该应用程序,而不会调用上面显示的处理函数.

However, if the app is terminated (app not launched yet), by clicking the URL, it only launches the app without calling the handling function that is shown above.

搜索后,我设法获得的最佳结果是

After searching through, the best result i manage to get is this

应用程序:WillFinishLaunchingWithOptions: 当要求打开URL时,此方法的返回结果与application:didFinishLaunchingWithOptions:方法的返回结果合并以确定是否应处理URL.如果这两个方法均返回NO,则系统不会调用application:openURL:options:方法.如果您不实现其中一种方法,则仅考虑已实现方法的返回值.

application:WillFinishLaunchingWithOptions: When asked to open a URL, the return result from this method is combined with the return result from the application:didFinishLaunchingWithOptions: method to determine if a URL should be handled. If either method returns NO, the system does not call the application:openURL:options: method. If you do not implement one of the methods, only the return value of the implemented method is considered.

-应用程序:didFinishLaunchingWithOptions: 此方法表示您最后一次机会来处理launchOptions词典中的任何键.如果未评估application:willFinishLaunchingWithOptions:方法中的键,则应在此方法中查看它们并提供适当的响应. 不是应用程序委托的对象可以通过观察名为UIApplicationDidFinishLaunchingNotification的通知并访问通知的userInfo字典来访问相同的launchOptions字典值.该方法返回后不久将发送该通知. 该方法的返回结果与application:willFinishLaunchingWithOptions:方法的返回结果结合起来,以确定是否应处理URL.如果任何一个方法返回NO,则不处理URL.如果您不实现其中一种方法,则仅考虑已实现方法的返回值.

- application:didFinishLaunchingWithOptions: This method represents your last chance to process any keys in the launchOptions dictionary. If you did not evaluate the keys in your application:willFinishLaunchingWithOptions: method, you should look at them in this method and provide an appropriate response. Objects that are not the app delegate can access the same launchOptions dictionary values by observing the notification named UIApplicationDidFinishLaunchingNotification and accessing the notification’s userInfo dictionary. That notification is sent shortly after this method returns. The return result from this method is combined with the return result from the application:willFinishLaunchingWithOptions: method to determine if a URL should be handled. If either method returns NO, the URL is not handled. If you do not implement one of the methods, only the return value of the implemented method is considered.

尽管有解释,但我仍然不知道该怎么做,并且在网上找不到其他具体内容.

Despite the explanation, i still do not know how to do it and i couldn't find anything else concrete online.

谢谢

致谢

推荐答案

我同意Kaloyan的观点,从未在应用程序启动时调用"handleOpenURL".因此,您必须在didFinishLaunchingWithOptions的"launchOptions"中检查URL.

I agree with Kaloyan, "handleOpenURL" is never called at application launch. So you have to check for URL in "launchOptions" in didFinishLaunchingWithOptions.

但是

我采用了与Apple

I adopted the same solution as Apple example code for QuickActions (3D Touch). I keep the URL at launch in a variable, and I handle it in applicationDidBecomeActive:.

@interface MyAppDelegate ()
@property (nonatomic, strong) NSURL *launchedURL;
@end

@implementation MyAppDelegate

- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{
    self.launchedURL = [launchOptions objectForKey:UIApplicationLaunchOptionsURLKey];
    ...
}

- (void)applicationDidBecomeActive:(UIApplication *)application
{
    if (self.launchedURL) {
        [self openLink:self.launchedURL];
        self.launchedURL = nil;
    }
}

- (BOOL)  application:(UIApplication *)application
          openURL:(NSURL *)url
sourceApplication:(NSString *)sourceApplication
       annotation:(id)annotation
{
    NSURL *openUrl = url;

    if (!openUrl)
    {
        return NO;
    }
    return [self openLink:openUrl];
}

- (BOOL)openLink:(NSURL *)urlLink
{
    ...
}

@end

这篇关于使用URL启动应用程序,但未调用OpenUrl的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆