How to load URL on WKWebView?

39,345

Solution 1

First of all you need to import

import WebKit

Following code is enough to open URL with WKWebView

let webView = WKWebView(frame: <#AnyRect#>)
let link = URL(string:"https://developer.apple.com/videos/play/wwdc2019/239/")!
let request = URLRequest(url: link)
webView.load(request)

Solution 2

Use -

webView.load(URLRequest(url: URL(string: "https://www.google.com/")!))

Solution 3

For Objective-c

  1. Goto Target -> Frameworks, Libraries, and Embed content
  2. Add Framework "WebKit.framework"
  3. Open your file and add #import

implement code in the function that you want to load WKWebView

WKWebView *webView = [[WKWebView alloc] init];
webView.frame = self.view.bounds;
[self.view addSubview:webView];
NSURLRequest *request = [NSURLRequest requestWithURL:[NSURL URLWithString:@"https://www.apple.com"]];
[webView loadRequest:request];

Solution 4

Select Target -> General -> Linked Framework and Libraries -> click on + symbol -> search for webKit.framwork -> add

import WebKit
webView.load(URLRequest(url: URL(string: "https://apple.com")!))

Solution 5

First, import

import WebKit 

Create Outlet of your WKWebView

 @IBOutlet var webload: WKWebView!

And, Put Below code in your viewDidLoad() method & wherever you want in your custom Function etc..

 let web_url = URL(string:"https://stackoverflow.com/users/6824665/dhaval-gevariya")!
 let web_request = URLRequest(url: web_url)
 webload.load(web_request)
Share:
39,345

Related videos on Youtube

Jarvis The Avenger
Author by

Jarvis The Avenger

Love Swift!!!

Updated on September 11, 2020

Comments

  • Jarvis The Avenger
    Jarvis The Avenger over 3 years

    I am trying to load URL on WKWebView which contain the CSV file.

    When I tried it loading normally it was giving me an error: 'The file format is not supported / it might be corrupted'.

    Even mobile safari is also giving me the same error.

    Then I tried using MIME type with the following method of WKWebView:

       try! Data(ContentsOf: bulkUrl)
    
       webView.load(data, mimeType: "text/csv", characterEncodingName: "", baseURL: bulkUrl)
    

    It works but giving me plain text.

    Same thing I tried it with UIWebView its opening CSV file in the correct format.

    I am not getting why WKWebView is not able to open the same file. Any idea?

    Thanks in advance

    • Vyacheslav
      Vyacheslav about 6 years
      What's your url?
    • Jarvis The Avenger
      Jarvis The Avenger about 6 years
      @Vyacheslav Can't share URL due to security reasons. but when I tried to create URL using gist.github.com the URL is opening on WKWebview, with normal load URLRequest.
    • Jarvis The Avenger
      Jarvis The Avenger about 6 years
      But MIME type is not working its giving plain text. I will let you know once I find out the solution.