URLComponents finding nil



所以我的条件是,如果URL包含在com://******/sendto/webview那么它应该在我的应用程序内的浏览器中打开,否则它将在Safari中打开。我不知道我做错了什么?

        if let url = homeObject["deeplink_url"] as? String, url != "" {
            if url.contains("com://******/sendto/webview") {
                if url.contains("?url") {
                    self.fixMalformedURL(url)
                } else {
                    if let urlComponents = URLComponents(url: URL(string: url)!, resolvingAgainstBaseURL: true) {
                        let webViewTitle = urlComponents.queryItems!.filter({ $0.name == "title" }).first
                        let webViewURL = urlComponents.queryItems!.filter({$0.name == "url"}).first
                        let storyboard = UIStoryboard(name: "Main", bundle: nil)
                        let vc = storyboard.instantiateViewController(withIdentifier: "FeaturedWebViewController") as! FeaturedWebViewController
                        vc.webViewTitle = webViewTitle!.value
                        vc.dynamicURL = webViewURL!.value
                        self.navigationController?.pushViewController(vc, animated: true)
                    }
                }
            } else {
                UIApplication.shared.openURL(URL(string: url)!)
            }
        }

您的应用程序可能会崩溃,因为您尝试获取"标题"字段的过滤器,但它在 url 中不存在,因此这是工作代码,如果 url 有空格,然后删除它

let homeObject = ["deeplink_url":"com://www.xxxyyy.com?title=topPersons&url=google.com"]
    if let url = homeObject["deeplink_url"], url != "" {
      if url.contains("www.xxxyyy.com") {
        if url.contains("?url") {
         //asdj asd asd asdasdfasdf asdfa sdf asdf asdf asdf asdf a sdf asd asdghgjkkjkjkljkljkjkl jkljkljkl jklj jl jljasd asd asdf asd asdf asdf asdf asd asdf asdf aasasaasdasdfasdf asdf asdasdfasdf hoasdasd

        } else {


          if let urlComponents = URLComponents(url:URL.init(string: url)!, resolvingAgainstBaseURL: false)
          {

            let webViewTitle = urlComponents.queryItems!.filter({ $0.name == "title" }).first
            let webViewURL = urlComponents.queryItems!.filter({$0.name == "url"}).first
            let storyboard = UIStoryboard(name: "Main", bundle: nil)
            let vc = storyboard.instantiateViewController(withIdentifier: "FeaturedWebViewController") as! FeaturedWebViewController
            vc.webViewTitle = webViewTitle!.value
            vc.dynamicURL = webViewURL!.value
            self.navigationController?.pushViewController(vc, animated: true)
          }
        }
      } else {
        UIApplication.shared.openURL(URL(string: url)!)
      }

相关内容

  • 没有找到相关文章

最新更新