SFSafariViewController 崩溃:指定的 URL 有一个不受支持的方案
SFSafariViewController crash: The specified URL has an unsupported scheme
我的代码:
if let url = NSURL(string: "www.google.com") {
let safariViewController = SFSafariViewController(URL: url)
safariViewController.view.tintColor = UIColor.primaryOrangeColor()
presentViewController(safariViewController, animated: true, completion: nil)
}
这仅在初始化时崩溃,但有以下例外:
The specified URL has an unsupported scheme. Only HTTP and HTTPS URLs are supported
当我使用url = NSURL(string: "http://www.google.com")
时,一切都很好。
我实际上是从 API 加载 URL,因此,我不能确定它们是否会以 http(s)://
.
为前缀
如何解决这个问题?我应该始终检查并添加前缀 http://
,还是有解决方法?
您可以在创建 NSUrl
对象之前检查 url
字符串中 http 的可用性。
将下面的代码放在你的代码之前,它会解决你的问题(你也可以用同样的方式检查https
)
var strUrl : String = "www.google.com"
if strUrl.lowercaseString.hasPrefix("http://")==false{
strUrl = "http://".stringByAppendingString(strUrl)
}
在创建 SFSafariViewController
的实例之前尝试检查 URL
的方案。
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)
}
}
我结合了 Yuvrajsinh 和 hoseokchoi 的回答。
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)
}
使用WKWebView的方法(开始iOS11),
class func handlesURLScheme(_ urlScheme: String) -> Bool
你可以添加到
NSString* webStringURL = [url stringByAddingPercentEscapesUsingEncoding: NSUTF8StringEncoding];
NSURL *URL = [NSURL URLWithString: webStringURL];
我的代码:
if let url = NSURL(string: "www.google.com") {
let safariViewController = SFSafariViewController(URL: url)
safariViewController.view.tintColor = UIColor.primaryOrangeColor()
presentViewController(safariViewController, animated: true, completion: nil)
}
这仅在初始化时崩溃,但有以下例外:
The specified URL has an unsupported scheme. Only HTTP and HTTPS URLs are supported
当我使用url = NSURL(string: "http://www.google.com")
时,一切都很好。
我实际上是从 API 加载 URL,因此,我不能确定它们是否会以 http(s)://
.
如何解决这个问题?我应该始终检查并添加前缀 http://
,还是有解决方法?
您可以在创建 NSUrl
对象之前检查 url
字符串中 http 的可用性。
将下面的代码放在你的代码之前,它会解决你的问题(你也可以用同样的方式检查https
)
var strUrl : String = "www.google.com"
if strUrl.lowercaseString.hasPrefix("http://")==false{
strUrl = "http://".stringByAppendingString(strUrl)
}
在创建 SFSafariViewController
的实例之前尝试检查 URL
的方案。
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)
}
}
我结合了 Yuvrajsinh 和 hoseokchoi 的回答。
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)
}
使用WKWebView的方法(开始iOS11),
class func handlesURLScheme(_ urlScheme: String) -> Bool
你可以添加到
NSString* webStringURL = [url stringByAddingPercentEscapesUsingEncoding: NSUTF8StringEncoding];
NSURL *URL = [NSURL URLWithString: webStringURL];