How to load URL in UIWebView in Swift?

前端 未结 18 1417
南旧
南旧 2020-11-29 00:58

I have the following code:

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

I am getting the following error:

相关标签:
18条回答
  • 2020-11-29 01:07

    Loading URL to WebView is very easy. Just create a WebView in your storyboard and then you can use the following code to load url.

        let url = NSURL (string: "https://www.simplifiedios.net");
        let request = NSURLRequest(URL: url!);
        webView.loadRequest(request);
    

    As simple as that only 3 lines of codes :)

    Ref: UIWebView Example

    0 讨论(0)
  • 2020-11-29 01:10

    UIWebView in Swift

    @IBOutlet weak var webView: UIWebView!
    
    override func viewDidLoad() {
        super.viewDidLoad()
            let url = URL (string: "url here")
            let requestObj = URLRequest(url: url!)
            webView.loadRequest(requestObj)
        // Do any additional setup after loading the view.
    }
    

    /////////////////////////////////////////////////////////////////////// if you want to use webkit

    @IBOutlet weak var webView: WKWebView!
    override func viewDidLoad() {
        super.viewDidLoad()
    
        let webView = WKWebView(frame: CGRect(x: 0, y: 0, width: self.view.frame.size.width, height: self.webView.frame.size.height))
        self.view.addSubview(webView)
        let url = URL(string: "your URL")
        webView.load(URLRequest(url: url!))`
    
    0 讨论(0)
  • 2020-11-29 01:10
    import UIKit
    
    class ViewController: UIViewController{
    
    @IBOutlet weak var webView: UIWebView!
    
    
    
    override func viewDidLoad() {
       super.viewDidLoad()
           //Func Gose Here 
          loadUrl()
    
       } 
     func loadUrl(){
    
       let url = URL(string: "Url which you want to load(www.google.com)")
       let requestObj = URLRequest(url: url! as URL)
       webView.load(requestObj)
     }
    
    }
    
    0 讨论(0)
  • 2020-11-29 01:11

    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!!

    0 讨论(0)
  • 2020-11-29 01:11

    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

    0 讨论(0)
  • 2020-11-29 01:12

    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:

    0 讨论(0)
提交回复
热议问题