首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >如何使用URL打开Swift应用程序并传递数据?

如何使用URL打开Swift应用程序并传递数据?
EN

Stack Overflow用户
提问于 2014-11-20 16:58:13
回答 2查看 1.4K关注 0票数 3

我试图让我的应用程序能够捕获启动它的url,并解析传递的值。

这是如何在ObjC中完成的

代码语言:javascript
复制
- (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);
}

到目前为止我拥有的是..。

代码语言:javascript
复制
func handleGetURLEvent(event: NSAppleEventDescriptor?, replyEvent: NSAppleEventDescriptor?) {

}
EN

回答 2

Stack Overflow用户

回答已采纳

发布于 2014-11-22 04:33:58

在浏览了几天后.我找到了这个解决方案。

代码语言:javascript
复制
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)的函数名不匹配。第二个问题是找到正确的感叹号位置来展开选项。

如果你有同样的问题,希望这能帮到你。

票数 3
EN

Stack Overflow用户

发布于 2014-11-20 17:12:24

我想这就是你需要的

代码语言:javascript
复制
func handleGetURLEvent(event: NSAppleEventDescriptor?, replyEvent: NSAppleEventDescriptor?) {
    if let aeEventDescriptor = event?.paramDescriptorForKeyword(AEKeyword(keyDirectObject)) {
        let urlStr = aeEventDescriptor.stringValue
        println(urlStr)
    }
}
票数 2
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/27045252

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档