如何处理默认 URL 方案 [英] How to handle with a default URL scheme
问题描述
我想在我的应用中构建 URI(或 URL 方案)支持.
I want to build URI (or URL scheme) support in my app.
我在我的 + (void)initialize
中做了一个 LSSetDefaultHandlerForURLScheme()
并且我也在我的 info.plist
中设置了特定的 URL 方案.所以我有没有 Apple Script
或 Apple Events
的 URL 方案.
I do a LSSetDefaultHandlerForURLScheme()
in my + (void)initialize
and I setted the specific URL schemes also in my info.plist
. So I have URL schemes without Apple Script
or Apple Events
.
当我在我最喜欢的浏览器中调用 myScheme:
时,系统会激活我的应用.
When I call myScheme:
in my favorite browser the system activates my app.
问题是,如何在调用时处理方案.或者更好地说:当 myScheme:
被调用时,我如何定义我的应用程序应该做什么.
The problem is, how to handle the schemes when they are called. Or better said: How can I define what my app should do, when myScheme:
is called.
是否有我必须实施的特殊方法或我必须在某处注册一个方法?
Is there a special method that I have to implement or do I have to register one somewhere?
推荐答案
当您提到 AppleScript 时,我想您正在使用 Mac OS X.
As you are mentioning AppleScript, I suppose you are working on Mac OS X.
注册和使用自定义 URL 方案的一种简单方法是在您的 .plist 中定义方案:
A simple way to register and use a custom URL scheme is to define the scheme in your .plist:
<key>CFBundleURLTypes</key>
<array>
<dict>
<key>CFBundleURLName</key>
<string>URLHandlerTestApp</string>
<key>CFBundleURLSchemes</key>
<array>
<string>urlHandlerTestApp</string>
</array>
</dict>
</array>
要注册该方案,请将其放入您的 AppDelegate 的初始化中:
To register the scheme, put this in your AppDelegate's initialization:
[[NSAppleEventManager sharedAppleEventManager]
setEventHandler:self
andSelector:@selector(handleURLEvent:withReplyEvent:)
forEventClass:kInternetEventClass
andEventID:kAEGetURL];
每当您的应用程序通过 URL 方案被激活时,定义的选择器就会被调用.
Whenever your application gets activated via URL scheme, the defined selector gets called.
事件处理方法的存根,显示如何获取 URL 字符串:
A stub for the event-handling method, that shows how to get the URL string:
- (void)handleURLEvent:(NSAppleEventDescriptor*)event
withReplyEvent:(NSAppleEventDescriptor*)replyEvent
{
NSString* url = [[event paramDescriptorForKeyword:keyDirectObject]
stringValue];
NSLog(@"%@", url);
}
Apple 的文档:安装获取 URL 处理程序
更新我刚刚注意到在 applicationDidFinishLaunching:
中安装事件处理程序的沙盒应用程序存在问题.启用沙箱后,在通过单击使用自定义方案的 URL 启动应用程序时,不会调用处理程序方法.通过稍早安装处理程序,在 applicationWillFinishLaunching:
中,该方法将按预期调用:
Update
I just noticed a problem for sandboxed apps that install the event handler in applicationDidFinishLaunching:
. With enabled sandboxing, the handler method doesn't get called when the app is launched by clicking a URL that uses the custom scheme.
By installing the handler a bit earlier, in applicationWillFinishLaunching:
, the method gets called as expected:
- (void)applicationWillFinishLaunching:(NSNotification *)aNotification
{
[[NSAppleEventManager sharedAppleEventManager]
setEventHandler:self
andSelector:@selector(handleURLEvent:withReplyEvent:)
forEventClass:kInternetEventClass
andEventID:kAEGetURL];
}
- (void)handleURLEvent:(NSAppleEventDescriptor*)event
withReplyEvent:(NSAppleEventDescriptor*)replyEvent
{
NSString* url = [[event paramDescriptorForKeyword:keyDirectObject]
stringValue];
NSLog(@"%@", url);
}
<小时>
在 iPhone 上,处理 URL-scheme 激活的最简单方法是实现 UIApplicationDelegate 的 application:handleOpenURL:
- 文档
这篇关于如何处理默认 URL 方案的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!