仅当应用程序未处于活动状态时,才在 openURL 函数之后调用方法



访问网站上的共享 URL 后,系统会提示查看者在我的应用程序中打开该链接。如果应用程序正在运行,则它可以完美运行。

但是,如果应用程序已关闭,则不会在我的 ViewController 上调用viewDidLoadviewWillAppear方法,因此我无法打开所需的 ViewController。

有谁知道如何让viewDidLoad函数运行,如果应用程序从openURL函数启动?

我目前有:

应用代表:

-(BOOL) application:(UIApplication *)application openURL:(NSURL *)url sourceApplication:(NSString *)sourceApplication annotation:(id)annotation {
if (!url) { return NO; }
NSString *party_url = [NSString stringWithFormat:@"%@%@/", PARTYURL, url.lastPathComponent];
[[NSNotificationCenter defaultCenter] postNotification:[NSNotification notificationWithName:@"WebViewNotification" object:party_url]];
return YES;
}

视图控制器:

- (void)viewDidLoad {
[super viewDidLoad];
appDelegate = [AppDelegate getDelegate];
[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(webViewNotification:) name:@"WebViewNotification" object:nil];
}
- (void)webViewNotification:(NSNotification *)notification {
NSString *url = [notification object];
PartyViewController *partyViewController = [self.storyboard instantiateViewControllerWithIdentifier:@"PartyViewController"];
partyViewController.partyUrl = url;
[self.navigationController pushViewController:partyViewController animated:YES];
}

同样,如果以前打开过应用程序,则此方法可以正常工作。但是,如果它被关闭,这不起作用。谢谢!

当你的应用在没有任何其他操作的情况下从链接启动时,你没有呈现的视图控制器在视图堆栈中包含此代码。 所以这段代码永远不会被执行。 考虑直接在 CustomTabBarController 中呈现 partyViewController。

最新更新