Creating a simple browser with WKWebView in Swift.

The UIWebView which is part of UIKit. UIWebView class has been around iOS 2.0 and its is most successfully browser to display HTML content inside the apps.
and its very simple to open URL in your apps using UIWebView. 

Now, After  iOS 8 and Later, Apple introduce new class WKWebView in WebKit. WKWebView is more faster to load webpage and more efficiently rather than UIWebView. WKWebView run as Separate process to the apps. Don't have memory overhead in apps.  

Step 1 : In ViewController.swift file, import the WebKit framework.
import WebKit
Step 2 : Add to the WKWebView variable in the class ViewController.swift.
var webView : WKWebView!
Step 3 : Adding the delegate WKNavigationDelegate in ViewController.swift file.
class ViewController: UIViewController , WKNavigationDelegate{
Step 4 :  Take the default url which want to load in WKWebView. In this tutorial, I want to load my blog url MYURL in WKWebView. also init the WKWebView in ViewDidLoad.
    // loading URL :
    let myBlog = ""
    let url = NSURL(string: myBlog)
    let request = NSURLRequest(URL: url!)

    // init and load request in webview.
    webView = WKWebView(frame: self.view.frame)
    webView.navigationDelegate = self
Step 5 :  When loading the URL in WKWebView, The below WKNavigationDelegate method is Called.
//MARK:- WKNavigationDelegate

func webView(webView: WKWebView, didFailProvisionalNavigation navigation: WKNavigation!, withError error: NSError) {
func webView(webView: WKWebView, didStartProvisionalNavigation navigation: WKNavigation!) {
    print("Strat to load")
func webView(webView: WKWebView, didFinishNavigation navigation: WKNavigation!) {
    print("finish to load")

Loading Url in WKWebView.


Creating a simple browser with WKWebView in Swift. Creating a simple browser with WKWebView in Swift. Reviewed by Kirit Modi on 23:44:00 Rating: 5

No comments:

Powered by Blogger.