【问题标题】:How do I open my Swift app with a URL and pass data?如何使用 URL 打开我的 Swift 应用程序并传递数据?
【发布时间】:2014-11-20 16:58:13
【问题描述】:

我试图让我的应用程序能够捕获启动它的 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?) {

}

【问题讨论】:

    标签: objective-c cocoa url swift uri


    【解决方案1】:

    在网上搜索了几天后......我找到了这个解决方案。

    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)
    }
    

    我的代码中有 2 个问题。首先,我的选择器与获取 url (handleGetURLEvent) 的函数名称不匹配。第二个问题是找到正确的感叹号位置来展开选项。

    如果您遇到同样的问题,希望这对您有所帮助。

    【讨论】:

      【解决方案2】:

      我想这就是你需要的

      func handleGetURLEvent(event: NSAppleEventDescriptor?, replyEvent: NSAppleEventDescriptor?) {
          if let aeEventDescriptor = event?.paramDescriptorForKeyword(AEKeyword(keyDirectObject)) {
              let urlStr = aeEventDescriptor.stringValue
              println(urlStr)
          }
      }
      

      【讨论】:

      • 我收到一个错误:“Int”不能转换为“AEKeyword”
      • 我遇到了同样的错误。 @BrianPeters 你有没有想过这个?
      • 希望它现在对你正常工作,@CliftonLabrum
      猜你喜欢
      • 2012-12-23
      • 2014-10-29
      • 2017-01-10
      • 1970-01-01
      • 1970-01-01
      • 2021-02-06
      • 2014-04-15
      • 2016-06-22
      • 1970-01-01
      相关资源
      最近更新 更多