Swift中的UIWebView问题[重复]
Posted
技术标签:
【中文标题】Swift中的UIWebView问题[重复]【英文标题】:UIWebView Issue in Swift [duplicate] 【发布时间】:2016-01-16 02:21:31 【问题描述】:我的 UIWebView 将无法加载,我非常困惑为什么。Webview 几周前还在工作。它链接到的站点已更新。但是由于某种原因,现在我发送给它的任何链接都不起作用。我尝试清除缓存。我有点迷失为什么这不会出现。我手机上的互联网工作正常。故事板中的所有内容都已正确连接。
活动指示器一直在旋转。我的警报来自应该发生的 didFailLoadWithError,但很高兴弄清楚它为什么无法连接。任何帮助都会很棒,谢谢。
class CommunityViewController: UIViewController, UIWebViewDelegate
@IBOutlet weak var activityIndicator: UIActivityIndicatorView!
@IBOutlet weak var communityWeb: UIWebView!
var refreshControl:UIRefreshControl!
let url = "http://www.google.com"
override func viewWillAppear(animated: Bool)
NSURLCache.sharedURLCache().removeAllCachedResponses()
NSURLCache.sharedURLCache().diskCapacity = 0
NSURLCache.sharedURLCache().memoryCapacity = 0
override func viewDidLoad()
super.viewDidLoad()
self.communityWeb.delegate = self
let requestURL = NSURL(string:url)
let request = NSURLRequest(URL: requestURL!, cachePolicy: NSURLRequestCachePolicy.ReloadIgnoringLocalAndRemoteCacheData,
timeoutInterval: 3.0)
communityWeb.loadRequest(request)
self.refreshControl = UIRefreshControl()
self.refreshControl.attributedTitle = NSAttributedString(string: "")
self.refreshControl.addTarget(self, action: "refresh:", forControlEvents: UIControlEvents.ValueChanged)
self.communityWeb.scrollView.addSubview(refreshControl)
func refresh(sender:AnyObject)
let requestURL = NSURL(string:url)
let request = NSURLRequest(URL: requestURL!)
communityWeb.loadRequest(request)
refreshControl.endRefreshing()
func webViewDidStartLoad(webView: UIWebView) // here show your indicator
self.activityIndicator.startAnimating()
func webViewDidFinishLoad(webView: UIWebView)
self.activityIndicator.stopAnimating()
self.activityIndicator.hidesWhenStopped = true
self.communityWeb.scrollView.contentSize.width = self.communityWeb.frame.size.width
func webView(webView: UIWebView, didFailLoadWithError error: NSError?)
let alertView = SIAlertView(title: "Internet Connection", andMessage: "Connect to the internet to receive latest updates from the Community")
alertView.addButtonWithTitle("OK", type: SIAlertViewButtonType.Default, handler:
alertView in
NSLog("pressed")
)
alertView.transitionStyle = SIAlertViewTransitionStyle.Fade
alertView.show()
【问题讨论】:
【参考方案1】:我想你忘记更新你的 info.plist
文件了。
将此密钥添加到您的文件中:
懒惰的选择是:
<key>NSAppTransportSecurity</key>
<dict>
<key>NSAllowsArbitraryLoads</key>
<true/>
</dict>
或者您可以直接将其添加到您的 info.plist
中,它看起来像:
您可以添加一个特定的域,例如:
<key>NSAppTransportSecurity</key>
<dict>
<key>NSExceptionDomains</key>
<dict>
<key>yourserver.com</key>
<dict>
<!--Include to allow subdomains-->
<key>NSIncludesSubdomains</key>
<true/>
<!--Include to allow HTTP requests-->
<key>NSTemporaryExceptionAllowsInsecureHTTPLoads</key>
<true/>
<!--Include to specify minimum TLS version-->
<key>NSTemporaryExceptionMinimumTLSVersion</key>
<string>TLSv1.1</string>
</dict>
</dict>
</dict>
【讨论】:
除了 PList 中有 NSAllowsArbitraryLoads 的苹果之外,苹果会不会?在论坛上阅读时,我得到了不同的答案。以上是关于Swift中的UIWebView问题[重复]的主要内容,如果未能解决你的问题,请参考以下文章
UIWebView 中的链接被视为 UIWebViewNavigationTypeOther
带有iOS swift的Facebook SDK未在UIWebView中打开[重复]
使用 UIWebView+AFNetworking 时链接如下