如何注册一个自定义应用程序开放的 URL 方案与 Xcode 4?

Xcode4需要大量的参数来使这个简单的事情成为可能:

NSString *stringURL = @"twitterriffic://";
NSURL *url = [NSURL URLWithString:stringURL];
[[UIApplication sharedApplication] openURL:url];

Xcode 4 Info.plist editor

这些房子是干什么用的?为什么是图像?我必须在这里重复应用程序标识符吗?如果我希望有人能够调用这个 URL 来打开我的应用程序,我应该选择什么角色?这些附加的 url 类型属性是用来做什么的?

我没有找到任何与 Xcode4相关的教程如何在 Xcode 4中注册这样的 URL 模式。

97685 次浏览

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.

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.

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.

Edit your AppName-Info.plist file

  • 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

Now if I install this app on my simulator, and open Safari and type "goomzee://" in the address bar it will launch my app.

Yup, this stuff isn't straightforward is it ?

I've outlined the steps required to register a custom URL here: Custom URLs

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:

URL types

In this example, I've registered the MKB prefix, 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):

Mail app

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")

The only gotcha is that I have yet to work out how to test whether a user has an iPad app installed which can recognise a particular prefix.

If they haven't, and they tap on one of these MKB:// links on their iPad, they'll get an ugly error message:

Nope