如何处理已打开的应用程序中的自定义 URL?

发布于 2024-10-31 11:32:31 字数 1691 浏览 1 评论 0原文


我有一个基于实用程序模板的应用程序(即主视图控制器和翻转视图控制器)。翻转视图允许选择要在主视图上使用的特定项目。到目前为止 - 效果很好。

现在我尝试添加自定义 URL。其效果如下: myapp://itemID=40 基本上会告诉主视图:“无需翻转 - 您将处理第 40 项”。

我将 URL 类型方案注册为“myapp”,并将以下方法添加到应用程序委托中:

- (BOOL)application:(UIApplication *)application handleOpenURL:(NSURL *)url {
    if (!url) {  
        return NO; 
    }

    NSString *urlString = [url absoluteString];
    NSLog(@"URL received: %@", urlString);
    NSString *itemID = [urlString stringByReplacingOccurrencesOfString:@"myapp://itemID=" withString:@""];
    NSLog(@"Item received: %@", itemID);
    [_mainViewController setStartupItem:itemID]; 
    return YES;
}

如您所见,itemID 设置为名为 的属性mainViewController 中的startupItem

然后,我向常规 application 方法添加一行,以验证在没有 URL 的情况下 startupItem 将为 nil

- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{
    //Make sure URL string is empty
    [_mainViewController setStartupItem:nil];

    // Override point for customization after application launch.
    // Add the main view controller's view to the window and display.
    self.window.rootViewController = self.mainViewController;
    [self.window makeKeyAndVisible];
    return YES;
}

在 MainViewController.m 中,我添加了代码来处理 viewDidLoad 事件中的项目。

我的问题是:如果应用程序第一次从 URL 启动,则此方案效果很好。如果它已经在运行,那么我们永远不会再次到达 viewDidLoad ,因此不会处理该特定项目,而是继续执行,就好像没有传递任何内容一样。

我的小问题是:我应该将处理代码放入哪个 UIViewController 中?或者,我是否以错误的方式处理这一切?这应该在我的模型中处理吗?

一如既往,提前感谢您的宝贵时间!

盖伊

I have an app based on the Utility template (i.e. Main and Flip view controllers). The Flip view allows selecting a certain item to be used on the main view. So far - works great.

Now I tried adding a custom URL. Something to the effect of: myapp://itemID=40 that will basically tell the main view: "no need to flip - you'll be dealing with item 40".

I registered the URL type scheme as "myapp" and added the following method to the app delegate:

- (BOOL)application:(UIApplication *)application handleOpenURL:(NSURL *)url {
    if (!url) {  
        return NO; 
    }

    NSString *urlString = [url absoluteString];
    NSLog(@"URL received: %@", urlString);
    NSString *itemID = [urlString stringByReplacingOccurrencesOfString:@"myapp://itemID=" withString:@""];
    NSLog(@"Item received: %@", itemID);
    [_mainViewController setStartupItem:itemID]; 
    return YES;
}

As you can see, the itemID is set to a property called startupItem in the mainViewController.

I then added one line to the regular application method to verify that startupItem will be nil in case of no URL:

- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{
    //Make sure URL string is empty
    [_mainViewController setStartupItem:nil];

    // Override point for customization after application launch.
    // Add the main view controller's view to the window and display.
    self.window.rootViewController = self.mainViewController;
    [self.window makeKeyAndVisible];
    return YES;
}

And in MainViewController.m I added code to handle the item to the viewDidLoad event.

And here's my problem: this scheme works great if the app is started from a URL the first time. If it's already running, then we never reach viewDidLoad again and therefore don't handle that particular item, but go on as if none was passed.

My humble question is: which UIViewController should I put my handling code in? Or, am I approaching all this the wrong way? Should this be handled in my model?

As always, thanks in advance for your time!

Guy

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

你曾走过我的故事 2024-11-07 11:32:31

我会具体查看 UIApplicationDelegate 协议的文档;

- (BOOL)application:(UIApplication *)application openURL:(NSURL *)url sourceApplication:(NSString *)sourceApplication annotation:(id)annotation

这已被弃用。

- (BOOL)application:(UIApplication *)application handleOpenURL:(NSURL *)url

I would take a look at the docs for UIApplicationDelegate protocol, specifically;

- (BOOL)application:(UIApplication *)application openURL:(NSURL *)url sourceApplication:(NSString *)sourceApplication annotation:(id)annotation

And this which is deprecated.

- (BOOL)application:(UIApplication *)application handleOpenURL:(NSURL *)url
断肠人 2024-11-07 11:32:31

绝对不是在应用程序启动时仅调用一次的方法!您需要在其自己的方法中重构项目处理代码,然后从 viewDidLoad 中调用此方法(在启动期间一次)并在每次调用时调用handleOpenURL。

Well definitely not in a method that gets called only once while the application starts up! You need to refactor the item handling code in its own method, then call this method from viewDidLoad (once during startup) and handleOpenURL each time it gets called.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文