UIWebView开始加载但从未完成

时间:2016-06-11 22:25:06

标签: ios swift uiwebview

我的UIWebView开始加载网页,但从未完成让我留下白页。我的代码在下面

import UIKit

class WebsiteController: UIViewController, UIWebViewDelegate {
    var index: Int = Int();
    var postLink: String = String()

    @IBOutlet weak var Browser: UIWebView!
    override func viewDidLoad() {
        super.viewDidLoad()
        let defaults = NSUserDefaults.standardUserDefaults()
        let school = (defaults.objectForKey("School") as! String)
        let bundleidentifier = NSBundle.mainBundle().bundleIdentifier
        var links: [String] = [String]()
        if let aStreamReader = StreamReader(path: NSBundle.mainBundle().pathForResource(school, ofType: "txt")!)
        {
            defer {
                aStreamReader.close()
            }
            while let line = aStreamReader.nextLine() {
                links.append(line)
            }
        }
        let url: NSURL = NSURL(string: links[index].stringByAddingPercentEncodingWithAllowedCharacters(NSCharacterSet.URLPathAllowedCharacterSet())!)!
        let request: NSURLRequest = NSURLRequest(URL: url)
        Browser.loadRequest(request)
        Browser.delegate = self
        Browser.opaque = false
        Browser.backgroundColor = UIColor .clearColor()
        // Do any additional setup after loading the view.
    }

    override func didReceiveMemoryWarning() {
        super.didReceiveMemoryWarning()
        // Dispose of any resources that can be recreated.
    }
    func webViewDidStartLoad(webView: UIWebView) {
        print("starting")
    }
    func webViewDidFinishLoad(webView: UIWebView) {
        print("finishing")
    }

所以在控制台中,当我加载此视图时,我得到“开始”但从未“完成”

2 个答案:

答案 0 :(得分:0)

检查didFailLoadWithError方法中的错误。

答案 1 :(得分:0)

检查您的控制台,看看是否收到App Transport Security错误。我遇到了同样的问题。该网址正确且网络正常,webViewDidStartLoad(:_)被点击,但webViewDidFinishLoad(:_)从未被点击过。将网址打印到控制台后,我注意到此错误App Transport Security has blocked a cleartext HTTP (http://) resource load since it is insecure. Temporary exceptions can be configured via your app's Info.plist file.