How to load URL in UIWebView in Swift?

2020-01-26 04:29发布

I have the following code:

UIWebView.loadRequest(NSURLRequest(URL: NSURL(string: "google.ca")))

I am getting the following error:

'NSURLRequest' is not convertible to UIWebView.

Any idea what the problem is?

标签: swift ios8
18条回答
够拽才男人
2楼-- · 2020-01-26 04:48

Try This

let url = NSURL (string: "http://www.apple.com");
myWebView.loadRequest(NSURLRequest(URL: url!));
查看更多
相关推荐>>
3楼-- · 2020-01-26 04:51

Try this:

  1. Add UIWebView to View.

  2. Connect UIWebview outlet using assistant editor and name your "webview".

  3. UIWebView Load URL.

    @IBOutlet weak var webView: UIWebView!
    
    override func viewDidLoad() {
       super.viewDidLoad()
        // Your webView code goes here
       let url = URL(string: "https://www.example.com")
       let requestObj = URLRequest(url: url! as URL)
       webView.load(requestObj)
    }
    

And run the app!!

查看更多
叛逆
4楼-- · 2020-01-26 04:51

You can load a page like this :

let url: URL = URL(string:"https://google.com")!
webView.loadRequest(URLRequest.init(url: url))

Or the one-line approach :

webView.loadRequest(URLRequest.init(url: URL(string: "https://google.com")!))

webView is your outlet var.

查看更多
放我归山
5楼-- · 2020-01-26 04:54

Swift 3 doesn't use NS prefix anymore on URL and URLRequest, so the updated code would be:

let url = URL(string: "your_url_here")
yourWebView.loadRequest(URLRequest(url: url!))
查看更多
We Are One
6楼-- · 2020-01-26 04:54

Easy,Tested and working 100%

Import webkit :

import WebKit

Assign IBOutlet to webview:

var webView : WKWebView!

set delegate:

class ViewController: UIViewController , WKNavigationDelegate{

Write code on viewDidLoad():

// loading URL :
let myBlog = "https://stackoverflow.com/users/4600136/mr-javed-multani?tab=profile"
let url = NSURL(string: myBlog)
    let request = NSURLRequest(url: url! as URL)

    // init and load request in webview.
    webView = WKWebView(frame: self.view.frame)
    webView.navigationDelegate = self
    webView.load(request as URLRequest)
    self.view.addSubview(webView)
    self.view.sendSubview(toBack: webView)

Write delegate methods:

//MARK:- WKNavigationDelegate

func webView(webView: WKWebView, didFailProvisionalNavigation navigation: WKNavigation!, withError error: NSError) {
print(error.localizedDescription)
}


 func webView(webView: WKWebView, didStartProvisionalNavigation navigation: WKNavigation!) {
print("Strat to load")
    }

 func webView(webView: WKWebView, didFinishNavigation navigation: WKNavigation!) {
print("finish to load")
}

look like: enter image description here

查看更多
一纸荒年 Trace。
7楼-- · 2020-01-26 04:55

In Swift 4 or 4.2 You can use like:

  1. Add WKWebView & connect to you view comtroller.
  2. Your view is like bellow:

     import UIKit
     import WebKit
    
    class ViewController: UIViewController {
    
       @IBOutlet weak var wkwebview: WKWebView!
    
        override func viewDidLoad() {
           super.viewDidLoad()
    
          let request = URLRequest(url: URL(string: "**your URL**")!)        
          wkwebview?.load(request)
    
      }
    
    }
    
  3. Allow Allow Arbitrary Loads true info.plist

      <key>NSAppTransportSecurity</key>
         <dict>
           <key>Allow Arbitrary Loads</key>
              <true/>
        </dict>
    

    Note info.plist will look like bellow

enter image description here

查看更多
登录 后发表回答