URLComponents 发现 nil

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({ [=10=].name == "title" }).first
                        let webViewURL = urlComponents.queryItems!.filter({[=10=].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)!)
            }

        }

您的应用程序可能会崩溃,因为您尝试获取 "title" 字段的过滤器,但它在 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({ [=10=].name == "title" }).first
            let webViewURL = urlComponents.queryItems!.filter({[=10=].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)!)
      }