gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-12-03 16:02:07 25 4
gpt4 key购买 nike

我正在开发一个 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 将数据传递到我的应用中

最佳答案

在应用程序委托(delegate)的 -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
}

关于macos - 当 OS X 应用程序通过注册的 URL 方案启动时,如何访问完整的 URL?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/9731601/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com