如何处理默认的URL方案 [英] How to handle with a default URL scheme

查看:278
本文介绍了如何处理默认的URL方案的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想在我的应用程序中创建URI(或URL方案)支持。



我做一个 LSSetDefaultHandlerForURLScheme()在我的 +(void)initialize 和我设置了特定的URL方案也在我的 info.plist 。所以我有没有 Apple Script Apple Events 的网址方案。



当我在我最喜欢的浏览器中调用 myScheme:时,系统会激活我的应用程序。



问题是,当它们被调用时如何处理这些方案。或者更好地说:当 myScheme:被调用时,如何定义我的应用应该做什么。



一个特殊的方法,我必须实现或者我必须注册一个地方。

解决方案

正如你提到的AppleScript,您正在使用Mac OS X。



注册和使用自定义URL方案的一种简单方法是在.plist中定义方案:

 < key> CFBundleURLTypes< / key> 
< array>
< dict>
< key> CFBundleURLName< / key>
< string> URLHandlerTestApp< / string>
< key> CFBundleURLSchemes< / key>
< array>
< string> urlHandlerTestApp< / string>
< / array>
< / dict>
< / array>

要注册该方案,请将其放在AppDelegate的初始化中:

  [[NSAppleEventManager sharedAppleEventManager] 
setEventHandler:self
andSelector:@selector(handleURLEvent:withReplyEvent :)
forEventClass:kInternetEventClass
andEventID:kAEGetURL];

每当通过URL方案激活您的应用程序时,将调用定义的选择器。



事件处理方法的存根,显示如何获取URL字符串:

   - (void)handleURLEvent:(NSAppleEventDescriptor *)event 
withReplyEvent:(NSAppleEventDescriptor *)replyEvent
{
NSString * url = [[event paramDescriptorForKeyword:keyDirectObject]
stringValue];
NSLog(@%@,url);
}

Apple的文档:安装Get URL处理程序

更新
我只注意到在 applicationDidFinishLaunching中安装事件处理程序的沙盒应用程序的问题:。启用沙箱功能后,通过单击使用自定义方案的URL启动应用程序时,不会调用处理程序方法。
通过稍早安装处理程序,在 applicationWillFinishLaunching:中,该方法将按预期调用:

   - (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);
}






处理URL方案激活的最简单的方法是实现UIApplicationDelegate的 application:handleOpenURL: - 文档


I want to build URI (or URL scheme) support in my app.

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.

When I call myScheme: in my favorite browser the system activates my app.

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?

解决方案

As you are mentioning AppleScript, I suppose you are working on Mac OS X.

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>

To register the scheme, put this in your AppDelegate's initialization:

[[NSAppleEventManager sharedAppleEventManager]
    setEventHandler:self
        andSelector:@selector(handleURLEvent:withReplyEvent:)
      forEventClass:kInternetEventClass
         andEventID:kAEGetURL];

Whenever your application gets activated via URL scheme, the defined selector gets called.

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's documentation: Installing a Get URL Handler

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);
}


On the iPhone, the easiest way to handle URL-scheme activation is, to implement UIApplicationDelegate's application:handleOpenURL: - Documentation

这篇关于如何处理默认的URL方案的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆