【发布时间】:2017-09-10 06:08:16
【问题描述】:
访问网站上的共享 URL 后,系统会提示查看者在我的应用程序中打开该链接。如果应用程序正在运行,它可以完美运行。
但是,如果应用程序关闭,我的 ViewController 上不会调用 viewDidLoad 和 viewWillAppear 方法,因此我无法打开所需的 ViewController。
如果应用程序是从openURL 函数启动的,有谁知道如何让viewDidLoad 函数运行?
我目前有:
AppDelegate:
-(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];
}
同样,如果该应用程序之前已打开,这将正常工作。但是,如果它被关闭,这将不起作用。谢谢!
【问题讨论】:
-
只是猜测:我认为您需要在
openURL:中直接实例化并推送您的partyViewController,而不是发送通知。你是否在openURL:中设置了断点,它会被调用吗? -
@Koen 我尝试将它直接推到那里,但根据我的导航控制器的设置方式,它对我不起作用。
openURL:方法在两种情况下都被成功调用。 -
那么上面的代码在哪个ViewController中?
-
@Koen 我在呈现的 ViewController 下有一个 CustomTabBarController。呈现的 ViewController 包含上面显示的代码。
标签: ios objective-c nsnotificationcenter deep-linking openurl