WKWebView – Open external link websites in Safari

How can I open links for external websites in Safari and keep internal looks in the WKwebview? Also how can I open links with target= _blank that want to open in a new window. Right now the link is not doing anything…

 

//
//  ViewController.swift
//  WKWebView
//


import UIKit
import WebKit


class ViewController: UIViewController {


    @IBOutlet weak var webView: WKWebView!
    @IBOutlet weak var activityIndicator: UIActivityIndicatorView!
    
    override func viewDidLoad() {
        super.viewDidLoad()
        
        // Do any additional setup after loading the view, typically from a nib.
        let url = "https://www.example.com"
        let request = URLRequest(url: URL(string: url)!)
        self.webView.load(request)
        self.webView.addObserver(self, forKeyPath: #keyPath(WKWebView.isLoading), options: .new, context: nil)
        
        // Page Scrolling - false or true
        webView.scrollView.isScrollEnabled = false
        
    }


    override func observeValue(forKeyPath keyPath: String?, of object: Any?, change: [NSKeyValueChangeKey : Any]?, context: UnsafeMutableRawPointer?) {
        
        if keyPath == "loading" {
            
            if webView.isLoading {
                
                activityIndicator.startAnimating()
                activityIndicator.isHidden = false
                
            }else {
                
                activityIndicator.stopAnimating()
                activityIndicator.isHidden = true
                
            }
            
        }
        
    }


}

Powered by WPeMatico

You may also like...

Comments are closed.