使用 UIStoryBoard 处理基于自定义 URL 方案的屏幕 [英] Handle screens based on Custom URL Scheme using UIStoryBoard

查看:39
本文介绍了使用 UIStoryBoard 处理基于自定义 URL 方案的屏幕的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有两个应用程序命名为,

I have two applications named with,

  1. 发送数据应用
  2. ReceiveDataApp

这是我的 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;

但是,这里的问题是我想显示我从 SendDataApp 收到的数据到我的 ReceivedDataAppDetailViewController 我正在尝试下面的方法来处理它,

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

由于未捕获的异常NSGenericException"而终止应用程序,原因:找不到 segue 'detailIdentifier' 的导航控制器.Push segues 只能在源控制器由 UINavigationController 的实例管理时使用.

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.

我的两个视图控制器仅嵌入在 UINavigationController 中.而且,通常我可以通过按钮操作查看详细信息页面.但是,不是通过URL Scheme

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.

推荐答案

您在 AppDelegate 的 openURL 方法中实例化您的 ViewController 并且 ARC 立即释放视图控制器 openURL 方法返回.它的存活时间不够长,无法添加到导航控制器中

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

您真正需要做的是获取当前显示的视图控制器(它可能是也可能不是您的View 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.

这篇关于使用 UIStoryBoard 处理基于自定义 URL 方案的屏幕的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆