0

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.

Dávid Pásztor
  • 51,403
  • 9
  • 85
  • 116
Sham Dhiman
  • 1,348
  • 1
  • 21
  • 59
  • This has nothing to do with SwiftUI, since `UIApplication` is a pure `UIKit` type and you're having an issue with `UIApplication`, not SwiftUI. – Dávid Pásztor Mar 26 '21 at 12:05
  • `www.google.com` is a URL that has no scheme. `http://www.google.com` is a URL with the scheme `http`. `https://www.google.com` is a URL with the scheme `https`. `openURL()` requires its URLs to have schemes. – Rudedog Mar 26 '21 at 15:33

1 Answers1

2

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:)
Shadowrun
  • 3,572
  • 1
  • 15
  • 13