如何处理已打开的应用程序中的自定义 URL?
我有一个基于实用程序模板的应用程序(即主视图控制器和翻转视图控制器)。翻转视图允许选择要在主视图上使用的特定项目。到目前为止 - 效果很好。
现在我尝试添加自定义 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
我会具体查看 UIApplicationDelegate 协议的文档;
这已被弃用。
I would take a look at the docs for UIApplicationDelegate protocol, specifically;
And this which is deprecated.
绝对不是在应用程序启动时仅调用一次的方法!您需要在其自己的方法中重构项目处理代码,然后从 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.