Skip to content
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
45 changes: 24 additions & 21 deletions DEV-Simple/ViewController.swift
Original file line number Diff line number Diff line change
Expand Up @@ -28,14 +28,16 @@ class ViewController: UIViewController, WKNavigationDelegate {

let pushNotifications = PushNotifications.shared

let devToURLString = "https://dev.to"

struct UserData: Codable {
var id: Int
}

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
Expand Down Expand Up @@ -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!))
Expand Down Expand Up @@ -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() {
Expand Down