2017-05-11 31 views
0

我正在使用Swift 3製作一個簡單的webview。但運行後只得到空白屏幕。使用Swift在WebView中獲取空白屏幕

它不是打開我在url請求中的鏈接。

import UIKit 

class ViewController: UIViewController { 

@IBOutlet var webView: UIWebView! 

override func viewDidLoad() { 
    super.viewDidLoad() 

    let url = URL (string: "http://www.google.net"); 
    let request = URLRequest(url: url!); 
    webView.loadRequest(request); 
    // Do any additional setup after loading the view, typically from a nib. 
} 

override func didReceiveMemoryWarning() { 
    super.didReceiveMemoryWarning() 
    // Dispose of any resources that can be recreated. 
} 
} 
+0

你從故事板連線WebView? –

+0

是的,我已經做到了。 –

+0

添加一個委託並檢查委託方法: 可選公共func webViewDidFinishLoad(_ webView:UIWebView) 可選公共func webView(_WebView:UIWebView,didFailLoadWithError錯誤:錯誤)。 此外,您還需要在plist中添加應用程序傳輸安全設置。 – rameez

回答

0

您必須設置NSAllowsArbitraryLoads關鍵YESNSAppTransportSecurity字典在你的文件的.plist。

這樣的形象:

enter image description here

0

寫這段代碼的info.plist

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

譜寫viewdid負荷這段代碼

webView.opaque = false 
webView.backgroundColor = UIColor.clear 
0
webView.loadRequest(request as URLRequest) 

試試這個它可能有幫助..