Ios SFSafariViewController崩溃:指定的URL具有不受支持的方案
我的代码:Ios SFSafariViewController崩溃:指定的URL具有不受支持的方案,ios,swift,sfsafariviewcontroller,Ios,Swift,Sfsafariviewcontroller,我的代码: if let url = NSURL(string: "www.google.com") { let safariViewController = SFSafariViewController(URL: url) safariViewController.view.tintColor = UIColor.primaryOrangeColor() presentViewController(safariViewController, animated: true
if let url = NSURL(string: "www.google.com") {
let safariViewController = SFSafariViewController(URL: url)
safariViewController.view.tintColor = UIColor.primaryOrangeColor()
presentViewController(safariViewController, animated: true, completion: nil)
}
只有在以下情况下,才会在初始化时崩溃:
指定的URL具有不受支持的方案。仅支持HTTP和HTTPS URL
当我使用url=NSURL(字符串:http://www.google.com)
,一切正常。
我实际上是从API加载URL,因此,我不能确定它们是否会以http(s)://
作为前缀
如何解决这个问题?我是否应该始终检查
http://
并为其添加前缀,或者有解决方法?在创建NSUrl
对象之前,您可以检查url
字符串中http的可用性
将以下代码放在您的代码之前,它将解决您的问题(您也可以以相同的方式检查https
)
在创建
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)
}
}
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)
}
}
Swift 2:
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)
}
}
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)
}
}
我综合了尤夫拉辛赫和霍塞奥科奇的答案
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的方法(从iOS 11开始)
看这个链接可能对你有帮助,我查过了,它没有关系。我已经允许任意加载。这个问题是不允许连接,也不允许SFSafariController加载本地html。这有点让你希望有一个
SFSafariViewController.canOpen(url:)
-ish方法来检查受支持的url。是的,这是我在问题本身中提到的解决方案。我想知道是否有一些解决方法或更好的方法来解决这个问题?@SahilKapoor目前我找不到任何更好的解决方案,如果你能找到它,然后也提到它以供将来参考如何使用它的示例?WKWebView.handlesURLScheme(url.scheme?.lowercased()?“”)