19

我正在开发一个 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>

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

我希望能够使用诸如myscheme://do/something/awesome 之类的 URL 将数据传递到我的应用程序中

4

1 回答 1

24

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

[[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
}
于 2012-03-16T03:57:40.703 回答