ios 如何使用 Xcode 4 注册自定义应用程序打开 URL 方案?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/8201724/
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
How to register a custom app opening URL scheme with Xcode 4?
提问by openfrog
Xcode4 is asking for a huge number of arguments just to make this simple thing possible:
Xcode4 要求大量的参数只是为了使这个简单的事情成为可能:
NSString *stringURL = @"twitterriffic://";
NSURL *url = [NSURL URLWithString:stringURL];
[[UIApplication sharedApplication] openURL:url];
What are all these properties for? Why an image? Must I repeat the app identifier here? What role to choose if I want anyone to be able to call this URL to open my app? And what are these Additional url type properties for?
所有这些属性有什么用?为什么是图像?我必须在这里重复应用程序标识符吗?如果我希望任何人都能够调用此 URL 来打开我的应用程序,该选择什么角色?这些额外的 url 类型属性有什么用?
I found no Xcode4-related tutorial how to register such an URL scheme with Xcode 4.
我没有找到与 Xcode4 相关的教程,如何在 Xcode 4 中注册这样的 URL 方案。
回答by Mike S.
- Open "Supporting Files" (folder) on left and click the "YourAppName-Info.plist"
- Choose a row like "Bundle creator OS Type Code" and mouse over row and click the (+) symbol
- This creates a new row and type "URL types"
- Click the arror to left and see Item 0 and you'll rename the value in Item 0 to "URL Schemes" as shown
- Then edit the field in Item 0 and type in your prototocol; I typed in "goomzee" as shown
- 打开左侧的“支持文件”(文件夹)并单击“YourAppName-Info.plist”
- 选择像“Bundle creator OS Type Code”这样的一行,然后将鼠标悬停在该行上并单击 (+) 符号
- 这将创建一个新行并输入“URL 类型”
- 单击左侧的箭头并查看 Item 0,您将 Item 0 中的值重命名为“URL Schemes”,如图所示
- 然后编辑项目 0 中的字段并输入您的协议;如图所示,我输入了“goomzee”
Now if I install this app on my simulator, and open Safari and type "goomzee://" in the address bar it will launch my app.
现在,如果我在模拟器上安装此应用程序,然后打开 Safari 并在地址栏中输入“goomzee://”,它将启动我的应用程序。
回答by Mike Gledhill
Yup, this stuff isn't straightforward is it ?
是的,这东西不简单,是吗?
I've outlined the steps required to register a custom URL here: Custom URLs
我在此处概述了注册自定义 URL 所需的步骤: 自定义 URL
But, basically the key to it is setting up a "URL Types" value in your .plist file. Here's what it looks like in XCode 5:
但是,基本上它的关键是在 .plist 文件中设置“URL 类型”值。这是它在 XCode 5 中的样子:
In this example, I've registered the MKBprefix, so now, I can use this new type of URLs in hyperlinks on webpages, and emails (if I read an email in the iPad Mail app):
在这个例子中,我已经注册了MKB前缀,所以现在,我可以在网页和电子邮件的超链接中使用这种新类型的 URL(如果我在 iPad 邮件应用程序中阅读了一封电子邮件):
If a user taps on one of these links, my iPad app will start up, and I can access the full URL string, to extract the other parameters from the URL (eg "DocumentNumber=100")
如果用户点击这些链接之一,我的 iPad 应用程序将启动,我可以访问完整的 URL 字符串,从 URL 中提取其他参数(例如“DocumentNumber=100”)
The only gotcha is that I have yet to work out how to testwhether a user has an iPad app installed which can recognise a particular prefix.
唯一的问题是我还没有弄清楚如何测试用户是否安装了可以识别特定前缀的 iPad 应用程序。
If they haven't, and they tap on one of these MKB:// links on their iPad, they'll get an ugly error message:
如果他们没有,并且他们在 iPad 上点击这些 MKB:// 链接之一,他们将收到一条丑陋的错误消息:
回答by Robin Summerhill
You can continue to register your custom URL by editing your app's info.plist file (as shown in one of your previous questions). The new editor in Xcode 4 is supposed to be a convenience to make it easier to add the entries - all it is doing is making the same info.plist changes that you would do manually. If you want to use the new editor then you only need to fill in the 'Identifier' and the 'URL Schemes' boxes. You don't need an image, icon or additional URL properties.
您可以通过编辑应用程序的 info.plist 文件来继续注册您的自定义 URL(如您之前的问题之一所示)。Xcode 4 中的新编辑器应该是一种方便,可以更轻松地添加条目 - 它所做的只是进行与手动执行相同的 info.plist 更改。如果您想使用新的编辑器,那么您只需要填写“标识符”和“URL 方案”框。您不需要图像、图标或其他 URL 属性。
The other properties (icon etc) are not well documented but seem to be applicable only on Mac OS X and might be used in the Finder's Get Info dialog to display what types of URL a particular app can open. For example, take a look at the Launch Services Programming Guide.
其他属性(图标等)没有很好的文档记录,但似乎只适用于 Mac OS X,并且可以在 Finder 的“获取信息”对话框中使用以显示特定应用程序可以打开的 URL 类型。例如,查看Launch Services Programming Guide。
回答by Ying
What you change in that editor is also reflected in your project's info plist file. It takes the form of ProjectName-Info.plist. Hopefully that helps.
您在该编辑器中所做的更改也会反映在您项目的信息 plist 文件中。它采用ProjectName-Info.plist的形式。希望这有帮助。