Handle screens based on Custom URL Scheme using UIStoryBoard

纵然是瞬间 提交于 2019-12-11 20:29:20

问题


I have two applications named with,

  1. SendDataApp
  2. ReceiveDataApp

This is my StoryBoard of ReceiveDataApp

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.


回答1:


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.



来源:https://stackoverflow.com/questions/33821603/handle-screens-based-on-custom-url-scheme-using-uistoryboard

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!