Create an account

Very important

  • To access the important data of the forums, you must be active in each forum and especially in the leaks and database leaks section, send data and after sending the data and activity, data and important content will be opened and visible for you.
  • You will only see chat messages from people who are at or below your level.
  • More than 500,000 database leaks and millions of account leaks are waiting for you, so access and view with more activity.
  • Many important data are inactive and inaccessible for you, so open them with activity. (This will be done automatically)


Thread Rating:
  • 539 Vote(s) - 3.44 Average
  • 1
  • 2
  • 3
  • 4
  • 5
SFSafariViewController crash: The specified URL has an unsupported scheme

#1
My code:

if let url = NSURL(string: "www.google.com") {
let safariViewController = SFSafariViewController(URL: url)
safariViewController.view.tintColor = UIColor.primaryOrangeColor()
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: "http://www.google.com") `, everything is fine.
I am actually loading URL's from API and hence, I can't be sure that they will be prefixed with `http(s)://`.

How to tackle this problem? Should I check and prefix `http://` always, or there's a workaround?
Reply

#2
you can add to



NSString* webStringURL = [url stringByAddingPercentEscapesUsingEncoding: NSUTF8StringEncoding];
NSURL *URL = [NSURL URLWithString: webStringURL];
Reply

#3
Use WKWebView's method (starting iOS 11),


class func handlesURLScheme(_ urlScheme: String) -> Bool

Reply

#4
Try checking scheme of `URL` before making an instance of `SFSafariViewController`.

**Swift 3**:

func openURL(_ urlString: String) {
guard let url = URL(string: urlString) else {
// not a valid URL
return
}

if ["http", "https"].contains(url.scheme?.lowercased() ?? "") {
// Can open with SFSafariViewController
let safariViewController = SFSafariViewController(url: url)
self.present(safariViewController, animated: true, completion: nil)
} else {
// Scheme is not supported or no scheme is given, use openURL
UIApplication.shared.open(url, options: [:], completionHandler: nil)
}
}

**Swift 2**:

func openURL(urlString: String) {
guard let url = NSURL(string: urlString) else {
// not a valid URL
return
}

if ["http", "https"].contains(url.scheme.lowercaseString) {
// Can open with SFSafariViewController
let safariViewController = SFSafariViewController(URL: url)
presentViewController(safariViewController, animated: true, completion: nil)
} else {
// Scheme is not supported or no scheme is given, use openURL
UIApplication.sharedApplication().openURL(url)
}
}
Reply

#5
I did a combination of Yuvrajsinh's & hoseokchoi's answers.

func openLinkInSafari(withURLString link: String) {

guard var url = NSURL(string: link) else {
print("INVALID URL")
return
}

/// Test for valid scheme & append "http" if needed
if !(["http", "https"].contains(url.scheme.lowercaseString)) {
let appendedLink = "http://".stringByAppendingString(link)

url = NSURL(string: appendedLink)!
}

let safariViewController = SFSafariViewController(URL: url)
presentViewController(safariViewController, animated: true, completion: nil)
}
Reply

#6
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 = "www.google.com"
if strUrl.lowercaseString.hasPrefix("http://")==false{
strUrl = "http://".stringByAppendingString(strUrl)
}
Reply



Forum Jump:


Users browsing this thread:
1 Guest(s)

©0Day  2016 - 2023 | All Rights Reserved.  Made with    for the community. Connected through