Swift中的UIWebView问题

时间:2015-10-17 10:35:53

标签: ios swift uiwebview

我的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 个答案:

答案 0 :(得分:3)

我认为您忘记更新info.plist文件。

将此密钥添加到您的文件中:

懒惰选项是:

<key>NSAppTransportSecurity</key>
<dict>
    <key>NSAllowsArbitraryLoads</key>
    <true/>
</dict>

或者您可以直接将其添加到info.plist中,它看起来像:

enter image description here

您可以添加以下特定域名:

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