- (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将按照上述功能进行编码处理.
但是,如果应用程序被终止(应用程序尚未启动),通过单击URL,它只启动应用程序而不调用上面显示的处理函数.
搜索后,我得到的最好的结果是这个
应用:WillFinishLaunchingWithOptions:
当被要求打开一个URL时,该方法的返回结果与应用程序的返回结果相结合:didFinishLaunchingWithOptions:方法来确定是否应该处理URL.如果任一方法返回否,系统不调用应用程序:openURL:options:method.如果不实现其中一种方法,则只考虑已实现方法的返回值.
– 应用程序:didFinishLaunchingWithOptions:
此方法代表您最后一次处理launchOptions字典中任何键的机会.如果您没有评估应用程序中的键:willFinishLaunchingWithOptions:method,则应在此方法中查看它们并提供适当的响应.
不是应用程序委托的对象可以通过观察名为UIApplicationDidFinishLaunchingNotification的通知并访问通知的userInfo字典来访问相同的launchOptions字典值.该方法返回后不久就会发送该通知.
该方法的返回结果与应用程序的返回结果相结合:willFinishLaunchingWithOptions:方法来确定是否应该处理URL.如果任一方法返回NO,则不处理该URL.如果不实现其中一种方法,则只考虑已实现方法的返回值.
尽管有解释,我还是不知道该怎么做,我找不到任何具体的在线.
谢谢
问候
解决方法
然而
我采用与Apple example code相同的解决方案,用于QuickActions(3D Touch).我在发布时保持URL的变量,我在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