From 0121e8f6a3cbe0861223d96f16b21180f7c7ffed Mon Sep 17 00:00:00 2001 From: Ceri-anne Jackson Date: Sun, 11 Nov 2018 14:24:23 +0000 Subject: [PATCH] Refactor decidePolicyForNavigationAction function and extract dev.to url string as variable --- DEV-Simple/ViewController.swift | 45 ++++++++++++++++++--------------- 1 file changed, 24 insertions(+), 21 deletions(-) diff --git a/DEV-Simple/ViewController.swift b/DEV-Simple/ViewController.swift index 88e753b..138bffa 100644 --- a/DEV-Simple/ViewController.swift +++ b/DEV-Simple/ViewController.swift @@ -28,6 +28,8 @@ class ViewController: UIViewController, WKNavigationDelegate { let pushNotifications = PushNotifications.shared + let devToURLString = "https://dev.to" + struct UserData: Codable { var id: Int } @@ -35,7 +37,7 @@ class ViewController: UIViewController, WKNavigationDelegate { override func viewDidLoad() { webView.customUserAgent = "DEV-Native-ios" webView.scrollView.scrollIndicatorInsets.top = view.safeAreaInsets.top + 50 - let url = URL(string: "https://dev.to")! + let url = URL(string: devToURLString)! webView.load(URLRequest(url: url)) webView.allowsBackForwardNavigationGestures = true webView.navigationDelegate = self @@ -75,7 +77,7 @@ class ViewController: UIViewController, WKNavigationDelegate { @objc func updateWebView() { let appDelegate = UIApplication.shared.delegate as! AppDelegate let serverURL = appDelegate.serverURL - let url = URL(string: serverURL ?? "https://dev.to") + let url = URL(string: serverURL ?? devToURLString) DispatchQueue.main.asyncAfter(deadline: .now() + 0.2) { // Wait a split second if first launch (Hack, probably a race condition) self.webView.load(URLRequest(url: url!)) @@ -129,27 +131,28 @@ class ViewController: UIViewController, WKNavigationDelegate { func webView(_ webView: WKWebView, decidePolicyFor navigationAction: WKNavigationAction, decisionHandler: @escaping (WKNavigationActionPolicy) -> Swift.Void) { - // This is all pretty hacky. Basically we're opening non dev.to or oauth links in their own modal - do { - let url = navigationAction.request.url - if (url?.absoluteString)! == "about:blank" { - decisionHandler(.allow) - } else if (url?.absoluteString.hasPrefix("https://github.com/login"))! { - decisionHandler(.allow) - } else if (url?.absoluteString.hasPrefix("https://api.twitter.com/oauth"))! { - decisionHandler(.allow) - } else if (url!.host as! String != "dev.to") && navigationAction.navigationType.rawValue == 0 { - let storyboard = UIStoryboard(name: "Main", bundle: nil) - let controller = storyboard.instantiateViewController(withIdentifier: "Browser") as! BrowserViewController - controller.destinationUrl = navigationAction.request.url - self.present(controller, animated: true, completion: nil) - decisionHandler(.cancel) - } else { - decisionHandler(.allow) - } - } catch { + + guard let url = navigationAction.request.url else { decisionHandler(.allow) + return } + + let nonDevToLinkActivated = url.host != "dev.to" && navigationAction.navigationType.rawValue == 0 + + if nonDevToLinkActivated { + loadInBrowser(url: url) + decisionHandler(.cancel) + return + } + + decisionHandler(.allow) + } + + func loadInBrowser(url: URL) { + let storyboard = UIStoryboard(name: "Main", bundle: nil) + let controller = storyboard.instantiateViewController(withIdentifier: "Browser") as! BrowserViewController + controller.destinationUrl = url + self.present(controller, animated: true, completion: nil) } func populateUserData() {