1

So my condition here is that if the URL is contained com://******/sendto/webview then it should open in a browser inside my app or else it will open in Safari. I can't figure out what am I doing wrong?

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

        }
Eric Aya
  • 68,765
  • 33
  • 165
  • 232

1 Answers1

0

Your app may be crash because you try to get filter of "title" field but it filed does not exist in url so here is working code if url has whitespace then remove it

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)!)
      }
kirti Chavda
  • 2,983
  • 1
  • 15
  • 29
  • Example link :- com://www.xxxyyy.com?title=topPersons&url=http://google.com i have title inside my url still its crashing @kirti mali – Abhijit Hadkar Nov 03 '17 at 12:50
  • check updated code ,if url contain white space then remove it – kirti Chavda Nov 03 '17 at 13:25
  • i dont have any white spacing in my url. Is it breaking because the URLComponents() method wants proper URL like www.google.com?? – Abhijit Hadkar Nov 03 '17 at 14:08
  • no ,run above it working fine,tested in xcode 9 and swift4 – kirti Chavda Nov 03 '17 at 14:14
  • I fixed by adding percentEncoding to url "if let urlComponents = URLComponents.init(url: URL.init(string: url.addingPercentEncoding(withAllowedCharacters: NSCharacterSet.urlQueryAllowed)!)!, resolvingAgainstBaseURL: false)" thanks for the help @kirti mali – Abhijit Hadkar Nov 04 '17 at 05:37