How to load URL in UIWebView in Swift?

前端 未结 18 1416
南旧
南旧 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:13

    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!))
    
    0 讨论(0)
  • 2020-11-29 01:14

    UIWebView loadRequest: Create NSURLRequest using NSURL object, then passing the request to uiwebview it will load the requested URL into the web view.

     override func viewDidLoad() {
         super.viewDidLoad()
         // Do any additional setup after loading the view, typically from a nib.
         let url = NSURL (string: "http://www.google.com");
         let requestObj = NSURLRequest(URL: url!);
         myWebView.loadRequest(requestObj);
         self.view.addSubview(myWebView)
      }
    

    For more reference:

    http://sourcefreeze.com/uiwebview-example-using-swift-in-ios/

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

    For swift 4.2, 5+

    @IBOutlet weak var webView: UIWebView!
    
        override func viewDidLoad() {
           super.viewDidLoad()
               //Func Gose Here 
              let url = URL(string: link)
              let requestObj = URLRequest(url: url! as URL)
               webView.loadRequest(requestObj)
    
           }
    
    0 讨论(0)
  • 2020-11-29 01:21

    In Swift 3 you can do it like this:

    @IBOutlet weak var webview: UIWebView!
    
    override func viewDidLoad() {
        super.viewDidLoad()
    
        // Web view load
        let url = URL (string: "http://www.example.com/")
        let request = URLRequest(url: url!)
        webview.loadRequest(request)
    }
    

    Remember to add permission at Info.plist, add the following values:

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

    Swift 3

    @IBOutlet weak var webview: UIWebView!
    webview.loadRequest(URLRequest(url: URL(string: "https://www.yourvideo.com")!))
    
    0 讨论(0)
  • 2020-11-29 01:24

    Try This

    let url = NSURL (string: "http://www.apple.com");
    myWebView.loadRequest(NSURLRequest(URL: url!));
    
    0 讨论(0)
提交回复
热议问题