-1

I have two applications named with,

  1. SendDataApp
  2. ReceiveDataApp

This is my StoryBoard of ReceiveDataApp

ReceiveDataApp StoryBoard

I can able to send data to my receiving app and can handle it by below method,

- (BOOL)application:(UIApplication *)app openURL:(NSURL *)url options:(NSDictionary<NSString *,id> *)options;

But, here the problem is i want to show the data which i received from SendDataApp to my DetailViewController of ReceivedDataApp I am trying with below method to handle it,

Appdelegate.m

- (BOOL)application:(UIApplication *)app openURL:(NSURL *)url options:(NSDictionary<NSString *,id> *)options {
    UIStoryboard *storyBoard = [UIStoryboard storyboardWithName:@"Main" bundle:nil];
    ViewController *viewController = (ViewController *)[storyBoard instantiateViewControllerWithIdentifier:@"View"];
    [viewController receivedURL:url];
    return YES;
}

ViewController.m

- (void)receivedURL:(NSURL *)url {
    [self performSegueWithIdentifier:@"detailIdentifier" sender:url];
}

- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    if ([[segue identifier] isEqualToString:@"detailIdentifier"]) {
        DetailViewController *detail = [segue destinationViewController];
        detail.receivingURL = (NSURL *)sender;
    }
}

But, its giving me some error

Terminating app due to uncaught exception 'NSGenericException', reason: 'Could not find a navigation controller for segue 'detailIdentifier'. Push segues can only be used when the source controller is managed by an instance of UINavigationController.

My two viewcontrollers embedded in UINavigationController only. And, normally i can view detail page by button action. But, not by URL Scheme

What was the mistake here? And, am i doing anything wrong here?

Give me some suggestions or idea to handle this.

Praveenkumar
  • 24,084
  • 23
  • 95
  • 173

1 Answers1

0

You instantiate your ViewController in your AppDelegate's openURL method and ARC immediately releases the view controller when that openURL method returns. It doesn't survive long enough to be added to the navigation controller

What you really need to do is get the currently displayed view controller (which might or might not be your "View Controller") and then do your perform segue on that.

In other words, in your app delegate:

- (BOOL)application:(UIApplication *)app openURL:(NSURL *)url options:(NSDictionary<NSString *,id> *)options {

    UIViewController *rootvc = self.window.rootViewController;

    // make sure the root view controller is the ViewController class you want
    if ([rootvc isKindOfClass: [ViewController class]])
    {
        ViewController * vc = (ViewController *)rootvc;
        [vc receivedURL:url];
    }
    return YES;
}

You can see more of what I'm trying to do with answers in this question and this question.

Lastly, you really must rename your subclassed view controllers away from the super generic "View Controller" and "Detail View Controller" names.

Community
  • 1
  • 1
Michael Dautermann
  • 88,797
  • 17
  • 166
  • 215
  • 1
    And as you mentioned in chat, your root view controller was a navigation controller (as it will likely be for anyone using a navigation controller). So in that case you'll [use something like "`[self.navigationController visibleViewController]`" to get the current view controller](http://stackoverflow.com/questions/15634059/how-do-i-get-current-active-view-controller/29486154#29486154). – Michael Dautermann Nov 20 '15 at 10:55