Sahil Kapoor Sahil Kapoor - 10 months ago 368
iOS Question

SFSafariViewController crash: The specified URL has an unsupported scheme.

My code:

if let url = NSURL(string: "") {
let safariViewController = SFSafariViewController(URL: url)
safariViewController.view.tintColor = UIColor.wantoPrimaryOrangeColor()
presentViewController(safariViewController, animated: true, completion: nil)

This crashes on initialization only with exception:

The specified URL has an unsupported scheme. Only HTTP and HTTPS URLs are supported

When I use
url = NSURL(string: "")
, everything is fine.
I am actually loading URL's from API and hence, I can't be sure that they will be prefixed with

How to tackle this problem? Should I check and prefix
always, or there's a workaround?


You can check for availability of http in your url string before creating NSUrl object.

Put following code before your code and it will solve your problem (you can check for https also in same way)

var strUrl : String = ""
if strUrl.lowercaseString.hasPrefix("http://")==false{
     strUrl = "http://".stringByAppendingString(strUrl)