通过注册的URL方案启动OS X应用程序时,如何访问完整的URL? [英] When an OS X app is launched by a registered URL scheme, how do you access the full URL?

查看:95
本文介绍了通过注册的URL方案启动OS X应用程序时,如何访问完整的URL?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在开发一个Cocoa应用程序,该应用程序使用具有自定义方案的URL启动/激活,该自定义方案已在Info.plist文件中注册,如下所示:

I'm working on an Cocoa app which is launched/activated using URLs with a custom scheme which is registered in the Info.plist file like so:

<key>CFBundleURLTypes</key>
<array>
    <dict>
        <key>CFBundleURLName</key>
        <string>Open myscheme:// URLs</string>
        <key>CFBundleURLSchemes</key>
        <array>
            <string>myscheme</string>
        </array>
    </dict>
</array>

我的问题是,一旦启动或激活该应用程序,我如何知道启动该应用程序的URL?在iOS上,使用UIApplicationDelegate上的-application:openURL:sourceApplication:annotation:方法很容易,因为它是通过NSURL实例传递的.

My question is, once the app is launched or activated, how do I tell what the URL was that launched the app? On iOS, this is easy with the -application:openURL:sourceApplication:annotation: method on the UIApplicationDelegate since it is passed an NSURL instance.

我希望能够使用 myscheme://do/something/awesome

推荐答案

在您的应用程序委托的-applicationWillFinishLaunching:中,执行以下操作:

In your app delegate's -applicationWillFinishLaunching:, do:

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

handleAppleEvent:withReplyEvent:应该类似于:

- (void)handleAppleEvent:(NSAppleEventDescriptor *)event withReplyEvent:(NSAppleEventDescriptor *)replyEvent {
    NSString *urlString = [[event paramDescriptorForKeyword:keyDirectObject] stringValue];
    // do something with the URL string
}

这篇关于通过注册的URL方案启动OS X应用程序时,如何访问完整的URL?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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