Looking for some help with my iOS app. Within my app I am wanting to open clicked links from within my domain (EX: communionchapelefca.org) in WKWebView and then have links from all other domains (EX: google.com) open in Safari. I would prefer to do this progrmatically as well since that is how my code is already setup.
I have found a few solutions on stackoverflow (here, here, here, and here) but they all seem to be Obj-C based and I am looking for a solution using Swift. thanks in advance.
ViewController.swift:
import UIKit
import WebKit
class ViewController: UIViewController {
override func viewDidLoad() {
super.viewDidLoad()
let myWebView:WKWebView = WKWebView(frame: CGRectMake(0, 0, UIScreen.mainScreen().bounds.width, UIScreen.mainScreen().bounds.height))
myWebView.loadRequest(NSURLRequest(URL: NSURL(string: "http://www.communionchapelefca.org/app-home")!))
self.view.addSubview(myWebView)
Here is sample code from the response to the swift written in obj c.
Make a function to decide where to load the URL:
Usage:
For Swift 3.0
My swift 3 solution:
Do not forget also to setup te delegate
Swift 4 update for George Vardikos answer:
You can implement
WKNavigationDelegate
, add thedecidePolicyForNavigationAction
method and check there the navigationType and requested url. I have used google.com below but you can just change it to your domain:Xcode 8.3 • Swift 3.1 or later