2015-10-17 43 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