1

所以我的条件是,如果包含 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)!)
            }

        }
4

1 回答 1

0

您的应用程序可能会崩溃,因为您尝试获取“标题”字段的过滤器,但它在 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)!)
      }
于 2017-11-03T12:12:35.203 回答