使用URL启动应用程序,但OpenUrl未调用
我已经实现了一个URL方案,并使用它通过调用方法将数据传递给我的应用程序。 整个代码如下所示
- (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,它仅启动应用程序而不调用上面显示的处理函数。
经过搜索,我得到的最好结果就是这个
application:WillFinishLaunchingWithOptions: 当要求打开URL时,此方法的返回结果与application:didFinishLaunchingWithOptions:
的返回结果结合使用application:didFinishLaunchingWithOptions:
方法,以确定是否应该处理URL。 如果任一方法返回NO,则系统不会调用application:openURL:options
:method。 如果未实现其中一个方法,则仅考虑已实现方法的返回值。
– application:didFinishLaunchingWithOptions: 此方法表示您处理launchOptions字典中任何键的最后机会。 如果您没有评估application:willFinishLaunchingWithOptions:
的键application:willFinishLaunchingWithOptions:
方法,则应该在此方法中查看它们并提供适当的响应。 不是app委托的对象可以通过观察名为UIApplicationDidFinishLaunchingNotification
的通知并访问通知的userInfo字典来访问相同的launchOptions字典值。 该方法返回后不久发送该通知。 此方法的返回结果与application:willFinishLaunchingWithOptions:
的返回结果相结合application:willFinishLaunchingWithOptions:
方法,以确定是否应处理URL。 如果任一方法返回NO,则不处理URL。 如果未实现其中一个方法,则仅考虑已实现方法的返回值。
尽管有解释,我仍然不知道该怎么做,我在网上找不到具体的东西。
谢谢
问候
我同意Kaloyan的说法,“handleOpenURL”从未在应用程序启动时调用。 因此,您必须在didFinishLaunchingWithOptions中的“launchOptions”中检查URL。
然而
我采用了与QuickActions(3D Touch)的Apple 示例代码相同的解决方案。 我将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
嗨,当以前没有启动应用程序时,永远不会调用方法“handleOpenURL”。 您必须在didFinishLaunchingWithOptions中检查具有键“UIApplicationLaunchOptionsURLKey”的对象的“launchOptions”
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions { NSURL *url = [launchOptions objectForKey:UIApplicationLaunchOptionsURLKey]; //call function to handle the url like in handleURL, but do not call handleURL directly }
我相信现在有更好的答案,
-
application:handleOpenURL:
-
application:openURL:sourceApplication:annotation:
两者都在ios 9中弃用.Apple建议是:
使用
application:openURL:options:
相反。
application:openURL:options:
具有与旧版本不同的行为,因为它将在应用程序处于后台或将启动时执行 。
因此,您只需要处理其中的URL开头。 如下:
- (BOOL)application:(UIApplication *)app openURL:(NSURL *)url options:(NSDictionary *)options { // 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; }
对于iOS 10,请使用
func application(_ app: UIApplication, open url: URL, options: [UIApplicationOpenURLOptionsKey : Any] = [:]) -> Bool
Swift 2.x
func application(app: UIApplication, openURL url: NSURL, options: [String : AnyObject]) -> Bool { if (url.scheme == "yuvitime") { print("URL scheme:\(url.scheme)") let yuvitimeRequestValue = url.query! let userInfor = [ "YuvitimeRequest" : yuvitimeRequestValue ] let notificationCentre = NSNotificationCenter.defaultCenter() notificationCentre.postNotificationName("URLSCHEMEACTIVATEDNOTIFICATION", object: self, userInfo: userInfor) return true } else { return false } }