Search code examples
swifturluiapplicationurlopen

UIApplication.shared.open only works with https


I trying to open url with UIApplication.shared.open function but i'm little bit confused because its not working for www.google.com and its working for https://www.google.com.

struct GetIsoCode: View {
    @State var titile = "www.google.com"
    var body: some View {
        Text(titile)
            .onTapGesture {
                let urlS = URL(string:titile)
                if let url = urlS {
                    UIApplication.shared.open(url) { (response) in
                        print(response)
                    }
                }
            }
    }
}

First Use case:

Input url:

www.google.com

Output:

Failed to open URL www.google.com: Error Domain=NSOSStatusErrorDomain Code=-50 "invalid input parameters" UserInfo={NSDebugDescription=invalid input parameters, _LSLine=234, _LSFunction=-[_LSDOpenClient openURL:options:completionHandler:]}

Second Use case:

Input:

https://www.google.com

Result: It's working fine.

Can someone please explain to me how to show google from both links, I've tried to implement by above but no results yet.


Solution

  • The docs say:

    UIKit supports many common schemes, including the http, https, tel, facetime, and mailto schemes.

    Use this method to open the specified resource. If the specified URL scheme is handled by another app, iOS launches that app and passes the URL to it. (Launching the app brings the other app to the foreground.) If no app is capable of handling the specified scheme, the completion handler is called with the success parameter set to NO.

    To determine whether an app is installed that is capable of handling the URL, call the canOpenURL: method before calling this one

    1. Which URL scheme would the string "www.google.com" be?
    2. Call canOpenURL first, before calling open(url:)