0

After visiting a share url on a website, viewers are prompted to open the link in my application. If the application is running, it works perfectly.

However, if the application is closed, the viewDidLoad and viewWillAppear methods are not called on my ViewController, so I am not able to open the desired ViewController.

Does anyone know how to allow to get the viewDidLoad function to run if the app is launched from the openURL function?

I currently have:

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;
}

ViewController:

- (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];
}

Again, this works fine if the app was previously opened. However, if it is closed, this does not work. Thank you!

jape
  • 2,861
  • 2
  • 26
  • 58
  • Just a guess: I think you need to instantiate and push your partyViewController directly in `openURL:`, instead of sending the notification. Did you set a breakpoint in `openURL:`, does that get called? – koen Apr 14 '17 at 01:20
  • Possible duplicate of [iPhone/iPad. How do I launch an app from link in Safari?](http://stackoverflow.com/questions/2655366/iphone-ipad-how-do-i-launch-an-app-from-link-in-safari) – koen Apr 14 '17 at 01:21
  • @Koen I tried pushing it directly there, but it didn't work for me based on how my navigation controllers are setup. The `openURL:` method gets called successfully in both scenarios. – jape Apr 14 '17 at 01:21
  • So in which ViewController is the code above located? – koen Apr 14 '17 at 01:24
  • @Koen I have a CustomTabBarController underneath a presented ViewController. The presented ViewController contains the code shown above. – jape Apr 14 '17 at 01:26
  • BTW, the `openURL:` method above has been deprecated, you should consider using https://developer.apple.com/reference/uikit/uiapplicationdelegate/1623112-application?language=objc – koen Apr 14 '17 at 01:32

1 Answers1

1

When your app launched from a link without any other operation you don't have a presented ViewController contains this code in your views stacks. so this code will never been executed. consider directly present partyViewController in your CustomTabBarController.

Zhang
  • 394
  • 2
  • 11