Zarkex
Zarkex

Reputation: 51

NSURL WebView not loading

My WebView is not loading just blank white here is my code

//
//  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.
}


}

I already asked the same question a year ago and heard that i somehow need to enable a "security setting" in Xcode is this the same case here? Thanks in advance.

Upvotes: 2

Views: 1254

Answers (3)

Abhishek Mitra
Abhishek Mitra

Reputation: 3395

Well It is working perfectly: See the below code:

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

Edited

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
        }

Mainly I have added UIWebViewDelegate and its delegate method webViewDidFinishLoad . well try it, it will work definitely .

Upvotes: 1

Vamshi Krishna
Vamshi Krishna

Reputation: 989

Open your info.plist as SourceCode and add

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

Then do as mentioned below:

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

Upvotes: 1

Bala AP
Bala AP

Reputation: 37

Please add the following line in your info.plist(open as source code) file

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

For more info please check this Transport security has blocked a cleartext HTTP

Thanks

Upvotes: 1

Related Questions