【问题标题】:Methods not called after openURL function ONLY if app is not active仅当应用程序未处于活动状态时才在 openURL 函数后调用方法
【发布时间】:2017-09-10 06:08:16
【问题描述】:

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

但是,如果应用程序关闭,我的 ViewController 上不会调用 viewDidLoadviewWillAppear 方法,因此我无法打开所需的 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


【解决方案1】:

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

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2021-10-02
    • 2015-09-10
    • 1970-01-01
    • 2021-02-22
    • 1970-01-01
    • 1970-01-01
    • 2019-05-15
    • 1970-01-01
    相关资源
    最近更新 更多