当通过注册的URL方案启动OS X应用程序时,如何访问完整的URL?

pha*_*lat 17 url macos cocoa url-scheme launch-services

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

<key>CFBundleURLTypes</key>
<array>
    <dict>
        <key>CFBundleURLName</key>
        <string>Open myscheme:// URLs</string>
        <key>CFBundleURLSchemes</key>
        <array>
            <string>myscheme</string>
        </array>
    </dict>
</array>
Run Code Online (Sandbox Code Playgroud)

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

我希望能够通过myscheme:// do/something/awesome等URL将数据传入我的应用程序

jos*_*ber 23

在您的app delegate中-applicationWillFinishLaunching:,执行:

[[NSAppleEventManager sharedAppleEventManager] setEventHandler:self andSelector:@selector(handleAppleEvent:withReplyEvent:) forEventClass:kInternetEventClass andEventID:kAEGetURL];
Run Code Online (Sandbox Code Playgroud)

handleAppleEvent:withReplyEvent:应该是这个样子:

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