SFSafariViewController崩溃:指定的URL具有不受支持的方案 [英] SFSafariViewController crash: The specified URL has an unsupported scheme

查看:180
本文介绍了SFSafariViewController崩溃:指定的URL具有不受支持的方案的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的代码:

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:

指定的URL不受支持的方案.仅支持HTTP和HTTPS URL

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)://为前缀.

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)://.

如何解决这个问题?我应该始终检查并为http://加上前缀吗,还是有解决方法?

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

推荐答案

在创建NSUrl对象之前,您可以在url字符串中检查 http 的可用性.

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

在代码之前放置以下代码,它将解决您的问题(您也可以以相同的方式检查https)

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)
}

这篇关于SFSafariViewController崩溃:指定的URL具有不受支持的方案的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆