Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to load URL on WKWebView?

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

like image 843
Jarvis The Avenger Avatar asked Apr 03 '18 11:04

Jarvis The Avenger


3 Answers

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)
like image 134
Mr.Fingers Avatar answered Sep 26 '22 10:09

Mr.Fingers


Use -

webView.load(URLRequest(url: URL(string: "https://www.google.com/")!))
like image 32
Rahul Avatar answered Sep 22 '22 10:09

Rahul


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];
like image 30
Chea Sambath Avatar answered Sep 23 '22 10:09

Chea Sambath