0

I want to open a URL in the SFSafariViewController but it says that the URL is nil although it is not nil.

            let userURL = self.user?.url
            let url = URL(string: "https://www.\(userURL))")
            print(userURL)
            print(url)
            let webVC = SFSafariViewController(url: url!)
            self.present(webVC, animated: true, completion: nil)

For the print statesments i get the following:

print(userURL) -> Optional("linkedin.com/in/bob-tarantino-a5778194/")

print(URL) -> nil

Fatal error: Unexpectedly found nil while unwrapping an Optional value

The error is probably caused by using the variable "userURL" inside the URL String as it does not cause any errors if the URL contains a normal String but i do not know how to fix it.

1 Answers1

0

Since userURL is optional so please add nil checking as below to make sure that a non optional value is used in concatenation.

guard let nonEmptyURL = userURL else {
    print("Nil userURL")        
}

let url = URL(string: "https://www.\(nonEmptyURL))")

print(userURL)
print(url)
Sujal
  • 1,447
  • 19
  • 34