How to integrate WKWebView (Swift & Objective-C)

Introduction on WKWebView: As Apple was giving warning for use of UIWebView after uploading build to store from atleast 2 years approx. Now finally apple started rejecting builds for using of UIWebView. As UIWebView is deprecated since almost last 2 years. But most of the developers are not migrated to new WKWebView because apple was accepting builds instead of deprecated UIWebView but now things are changes apple made compulsory use of WKWebView in applications. If you will upload build with UIWebView then surely it is going to be rejected apple. Also, if there is any instance of it is mentioned in project then also it is going to be rejected.
Also, many third party library that we add in our project uses UIWebView. Apple also not accepting apps in which third party libraries are using UIWebView. Most of the third party library are already updated with new version with WKWebView in place of UIWebView. Many of them are converting so that user’s can use them otherwise there no mean to use such library when apple will going to reject you app.

How to integrate WKWebView (Swift & Objective-C) :

We are going to explan step-wise that how can you integrate WKWebView in you project. We will also cover here both Swift and Objective-C languages.
What we are going to do in this?
1. Create new blank project
2. Add WKWebView from Storyboard
3. Add WKWebView from Programmatically
4. Load HTML content in WKWebView
5. Load Webpage URL in WKWebView

1. Create new blank storyboard

Open Xcode and then choose option create new blank project.

Add WKWebView from Storyboard

STEP 1: Open Main.Storyboard file from project.
STEP 2: Go to ViewController file in that Storyboard. After that search for WKWebView from search elements options and then select it and add in ViewController file. Then give appropriate constraints or auto layout as per your requirements.
STEP 3:Add outlet of WKWebView in project and connect it from just added WKWebView in Storyboard that we have added in above step.

2. Add WKWebView from Programmatically

With Below code you can easily add WKWebView in your Controller file. Just added below mentioned code in ViewDidLoad or in any function that fits your requirement.

SwiftObjective-C
lazy var wkWebView: WKWebView = {
let webConfiguration = WKWebViewConfiguration()
let wkWebView = WKWebView(frame: .zero, configuration: webConfiguration)
wkWebView.uiDelegate = self
wkWebView.translatesAutoresizingMaskIntoConstraints = false
return wkWebView
}()
WKWebViewConfiguration *configuration = [[WKWebViewConfiguration alloc] init];
WKWebView *wkWebView = [[WKWebView alloc] initWithFrame:self.view.frame configuration:configuration];
wkWebView.navigationDelegate = self;
[self.view addSubview:wkWebView];

3. Load HTML content in WKWebView

Below code is for loading HTML content in WKWebview and also you can modify these line of code as per your requirement.

Read More:

SwiftObjective-C
let webView2 = WKWebView()
webView2.loadHTMLString(“Hello!”, baseURL: nil)
NSString *myHtml = [NSString stringWithContentsOfURL:url encoding:NSUTF8StringEncoding error:nil];
[wkWebView loadHTMLString:myHtml baseURL:nil];

4. Load Webpage URL in WKWebView

Below code is for loading Webpage URL content in WKWebview and also you can modify these line of code as per your requirement.

SwiftObjective-C
let loadURL = URL(string: “https://icodemate.com”)
let request = URLRequest(url: loadURL!)
wkWTebView.load(request)
NSURL *url=[NSURL URLWithString:@”https://icodemate.com”];
NSURLRequest *request =[NSURLRequest requestWithURL:url];
[wkWebView loadRequest:request];

Leave a Comment