从 URL 和传递参数打开 iOS 应用程序
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/14227288/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Open iOS app from URL AND Pass Parameters
提问by DHShah01
A link should open the app. I've got that to work. I just want to know how to pass a parameter. Let's say the url is "addappt://?code=abc". When a view controller pops up, a code field should have populated text - the letters after the equals to sign. I've got part of this to work. I use the following (in app delegate.m)
:
一个链接应该打开应用程序。我有这个工作。我只想知道如何传递参数。假设 url 是“addappt://?code=abc”。当视图控制器弹出时,代码字段应该填充文本 - 等号后面的字母。我有一部分工作要做。我使用以下内容 (in app delegate.m)
:
NSArray *elements = [url.query componentsSeparatedByString:@"="];
NSString *key = [[elements objectAtIndex:0] stringByReplacingPercentEscapesUsingEncoding:NSUTF8StringEncoding];
val = [[elements objectAtIndex:1] stringByReplacingPercentEscapesUsingEncoding:NSUTF8StringEncoding];
(BTW: val is declared in appdelegate.h
(顺便说一句:val 在 appdelegate.h 中声明
I am also able to pass val
to the view controller. My only problem is populating the textfield, named 'code'
. How can you populate code as soon as the app is opened by the link?
我也可以传递val
给视图控制器。我唯一的问题是填充名为'code'
. 如何在通过链接打开应用程序后立即填充代码?
Help Appreciated.
帮助赞赏。
采纳答案by Nikola Kirev
Here is a nice tutorial on Using Custom URL Scheme in iOS
这是一个关于在 iOS 中使用自定义 URL 方案的很好的教程
As in the tutorial, you should parse the URL parameters and store them to use in the app in this method:
在教程中,您应该解析 URL 参数并将它们存储在此方法中的应用程序中使用:
- (BOOL)application:(UIApplication *)application handleOpenURL:(NSURL *)url {
// Do something with the url here
}