2017-08-25 36 views
0

我加載URL在UIWebView如下:UIWebView的一片空白

import UIKit 

class ViewController: UIViewController { 

    @IBOutlet weak var MainWebView: UIWebView! 

    override func viewDidLoad() { 
     super.viewDidLoad() 
     let url = NSURL(string: "http://www.odysseynewsmagazine.net") 
     let request = NSURLRequest(url:url! as URL) 
     self.MainWebView!.loadRequest(request as URLRequest) 
    } 
} 

IBOutlet連接和我.plistApp Transport Security SettingsAllow Arbitrary Loads設置爲YES

爲什麼應用程序構建一個空白頁?

+0

沒錯。這是我的網絡連接不穩定。它運行良好。多謝你們! –

回答

0

嘗試

override func viewDidLoad() { 
    super.viewDidLoad() 

    let url = URL(string: "https://www.google.com")! // it works ? Comment line and uncomment the following one 
    // let url = URL(string: "http://www.odysseynewsmagazine.net")! 
    let request = URLRequest(url: url) 
    mainWebView.load(request) // var should start with lower case 
} 

如果一切正常,更改URL。如果它不適用於您的網址,那麼網站或ATS出現問題。粘貼控制檯輸出。

0

你的代碼很好!

你的問題是你已經錯過了你的NSAppTransportSecurity字典添加NSAllowsArbitraryLoadsInWebContent鍵和設置值設置爲TRUE

要解決這個問題,你只需要在你的plist中複製下面的圖片

enter image description here

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

您的webView呈現webView內容,您的代碼不變

enter image description here

希望這有助於

0

確定。因此,這裏是正常的Web視圖加載您的網址,只需在App運輸安全設置

@IBOutlet weak var webView: UIWebView! 

override func viewDidLoad() { 
    super.viewDidLoad() 
    // Do any additional setup after loading the view, typically from a nib. 
    let url = NSURL(string: "http://www.odysseynewsmagazine.net") 
    let request = NSURLRequest(url:url! as URL) 
    self.webView.loadRequest(request as URLRequest) 
} 

而且plist中設置設爲

允許任意負載YES。

enter image description here

而且結果是: enter image description here