我试图让我的应用程序能够捕获启动它的url,并解析传递的值。
这是如何在ObjC中完成的
- (void)handleGetURLEvent:(NSAppleEventDescriptor *)event withReplyEvent:(NSAppleEventDescriptor *)replyEvent
{
[event paramDescriptorForKeyword:keyDirectObject] ;
NSString *urlStr = [[event paramDescriptorForKeyword:keyDirectObject] stringValue];
// Now you can parse the URL and perform whatever action is needed
NSLog(@"URL: %@", urlStr);
}到目前为止我拥有的是..。
func handleGetURLEvent(event: NSAppleEventDescriptor?, replyEvent: NSAppleEventDescriptor?) {
}发布于 2014-11-22 04:33:58
在浏览了几天后.我找到了这个解决方案。
func applicationWillFinishLaunching(notification: NSNotification?) {
// -- launch the app with url
NSAppleEventManager.sharedAppleEventManager().setEventHandler(self, andSelector: Selector("handleGetURLEvent:withReplyEvent:"), forEventClass: AEEventClass(kInternetEventClass), andEventID: AEEventID(kAEGetURL))
}
func handleGetURLEvent(event: NSAppleEventDescriptor!, withReplyEvent: NSAppleEventDescriptor!) {
let urlPassed = NSURL(string: event.paramDescriptorForKeyword(AEKeyword(keyDirectObject))!.stringValue!)
println(urlPassed)
}我的代码中有两个问题。首先,我的选择器与获取url (handleGetURLEvent)的函数名不匹配。第二个问题是找到正确的感叹号位置来展开选项。
如果你有同样的问题,希望这能帮到你。
发布于 2014-11-20 17:12:24
我想这就是你需要的
func handleGetURLEvent(event: NSAppleEventDescriptor?, replyEvent: NSAppleEventDescriptor?) {
if let aeEventDescriptor = event?.paramDescriptorForKeyword(AEKeyword(keyDirectObject)) {
let urlStr = aeEventDescriptor.stringValue
println(urlStr)
}
}https://stackoverflow.com/questions/27045252
复制相似问题