2015-10-17 34 views
0

我的UIWebView不会加载,我非常困惑。为什么.Webview在几个星期前工作。它所链接的网站得到更新。但由于某种原因,现在我发送给它的任何链接都不起作用。我试图清除缓存。我有点失落,为什么这不会出现。我的手机上的互联网工作正常。故事板中的所有内容都正确连接。Swift中的UIWebView问题

活动指标保持旋转。而我的警报是从应该发生的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() 

} 

回答

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

苹果除了那个拥有NSAllowsArbitraryLoads的PList?在论坛阅读我得到了混合的答案。 – MangoCode