cocoa 应用接收某个URL Scheme
1)Info.plist添加URLtype
<key>CFBundleURLTypes</key>
<array>
<dict>
<key>CFBundleURLName</key>
<string>http URL</string>
<key>CFBundleURLSchemes</key>
<array>
<string>http</string>
</array>
</dict>
<dict>
<key>CFBundleURLName</key>
<string>Secure http URL</string>
<key>CFBundleURLSchemes</key>
<array>
<string>https</string>
</array>
</dict>
</array>
2) 注册apple event handler
- (void)registerMyApp
{
[[NSAppleEventManager sharedAppleEventManager] setEventHandler:self andSelector:@selector(getUrl:withReplyEvent:) forEventClass:kInternetEventClass andEventID:kAEGetURL];
}
- (void)getUrl:(NSAppleEventDescriptor )event withReplyEvent:(NSAppleEventDescriptor )replyEvent
{
NSString url = [[event paramDescriptorForKeyword:keyDirectObject] stringValue];
// Now you can parse the URL and perform whatever action is needed
}
3)设置缺省URL Scheme处理程序
CFStringRef bundleID = (CFStringRef)[[NSBundle mainBundle] bundleIdentifier];
OSStatus httpResult = LSSetDefaultHandlerForURLScheme(CFSTR("http"), bundleID);
OSStatus httpsResult = LSSetDefaultHandlerForURLScheme(CFSTR("https"), bundleID);
//TODO: Check httpResult and httpsResult for errors
缺省处理程序可以在下面的文件中查看
~/Library/Preferences/com.apple.LaunchServices/com.apple.launchservices.secure.plist
测试:
终端输入 open XXX://123