NSURL WebView无法加载

时间:2017-05-25 10:05:33

标签: ios swift webview uiwebview

我的WebView不是只加载空白,这是我的代码

//
//  SecondViewController.swift
//  Bachelmatt Garage
//
//  Created by Thilo Jaeggi on 24.05.17.
//  Copyright © 2017 Thilo Jaeggi. All rights reserved.
//

import UIKit

class SecondViewController: UIViewController {
@IBOutlet var webView: UIWebView!
override func viewDidLoad() {
    super.viewDidLoad()
    // Do any additional setup after loading the view, typically from a nib.
    let url = URL (string: "http://www.sourcefreeze.com");
    let request = URLRequest(url: url!);
    webView.loadRequest(request);
}


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


}

一年前我已经问了同样的问题并听说我需要启用一个"安全设置"在Xcode这是同样的情况吗?提前谢谢。

3 个答案:

答案 0 :(得分:1)

请在info.plist(作为源代码打开)文件中添加以下行

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

有关详细信息,请查看此Transport security has blocked a cleartext HTTP

由于

答案 1 :(得分:0)

将info.plist打开为SourceCode并添加

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

然后如下所述:

override func viewDidLoad() {
    super.viewDidLoad()
webView.loadRequest(URLRequest(url: URL(string: "http://www.sourcefreeze.com")!))
}

答案 2 :(得分:0)

嗯它工作正常:见下面的代码:

import UIKit
import SVProgressHUD

class ConditionsViewController: UIViewController, UIWebViewDelegate {

    @IBOutlet weak var webVw: UIWebView!


    override func viewDidLoad() {
        super.viewDidLoad()


        SVProgressHUD.show(withStatus: "Loading")

        webVw.delegate = self

        let url = URL (string: "http://www.sourcefreeze.com");
        let request = URLRequest(url: url!);
        webVw.loadRequest(request);
    }

    func webViewDidFinishLoad(_ webView: UIWebView) {

        SVProgressHUD.dismiss()
    }

enter image description here enter image description here

<强>被修改

import UIKit
   // import SVProgressHUD

    class ConditionsViewController: UIViewController, UIWebViewDelegate {

        @IBOutlet weak var webVw: UIWebView!


        override func viewDidLoad() {
            super.viewDidLoad()


            SVProgressHUD.show(withStatus: "Loading")

            webVw.delegate = self

            let url = URL (string: "http://www.sourcefreeze.com");
            let request = URLRequest(url: url!);
            webVw.loadRequest(request);
        }

        func webViewDidFinishLoad(_ webView: UIWebView) {

            //SVProgressHUD.dismiss()

            print("Loaded")

// Here it will print 2-3 sec later, in that mean time your page will load in your view controller
        }

主要是我添加了UIWebViewDelegate及其委托方法webViewDidFinishLoad。好好试试吧,它肯定会有效。