Skip to content

Commit

Permalink
Replace UIWebView by WKWebView on iOS Fix OAuthSwift#547
Browse files Browse the repository at this point in the history
  • Loading branch information
phimage committed Nov 1, 2019
1 parent 94af506 commit b40aebe
Show file tree
Hide file tree
Showing 2 changed files with 40 additions and 59 deletions.
5 changes: 5 additions & 0 deletions Demo/Common/AppDelegate.swift
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,11 @@ extension AppDelegate: UIApplicationDelegate {
applicationHandle(url: url)
return true
}

class var sharedInstance: AppDelegate {
return UIApplication.shared.delegate as! AppDelegate
}

}

#elseif os(OSX)
Expand Down
94 changes: 35 additions & 59 deletions Demo/Common/WebViewController.swift
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,8 @@ import OAuthSwift

#if os(iOS)
import UIKit
typealias WebView = UIWebView // WKWebView
import WebKit
typealias WebView = WKWebView
#elseif os(OSX)
import AppKit
import WebKit
Expand All @@ -24,21 +25,15 @@ class WebViewController: OAuthWebViewController {

override func viewDidLoad() {
super.viewDidLoad()


self.webView.frame = self.view.bounds
self.webView.navigationDelegate = self
self.webView.translatesAutoresizingMaskIntoConstraints = false
self.view.addSubview(self.webView)
self.view.addConstraints(NSLayoutConstraint.constraints(withVisualFormat: "|-0-[view]-0-|", options: [], metrics: nil, views: ["view":self.webView]))
self.view.addConstraints(NSLayoutConstraint.constraints(withVisualFormat: "V:|-0-[view]-0-|", options: [], metrics: nil, views: ["view":self.webView]))
#if os(iOS)
self.webView.frame = UIScreen.main.bounds
self.webView.scalesPageToFit = true
self.webView.delegate = self
self.view.addSubview(self.webView)
loadAddressURL()
#elseif os(OSX)

self.webView.frame = self.view.bounds
self.webView.navigationDelegate = self
self.webView.translatesAutoresizingMaskIntoConstraints = false
self.view.addSubview(self.webView)
self.view.addConstraints(NSLayoutConstraint.constraints(withVisualFormat: "|-0-[view]-0-|", options: [], metrics: nil, views: ["view":self.webView]))
self.view.addConstraints(NSLayoutConstraint.constraints(withVisualFormat: "V:|-0-[view]-0-|", options: [], metrics: nil, views: ["view":self.webView]))
loadAddressURL()
#endif
}

Expand All @@ -47,65 +42,46 @@ class WebViewController: OAuthWebViewController {
super.handle(url)
self.loadAddressURL()
}

func loadAddressURL() {
guard let url = targetURL else {
return
}
let req = URLRequest(url: url)
DispatchQueue.main.async {
#if os(iOS)
self.webView.loadRequest(req)
#elseif os(OSX)
self.webView.load(req)
#endif
}
}
}

// MARK: delegate
#if os(iOS)
extension WebViewController: UIWebViewDelegate {
func webView(_ webView: UIWebView, shouldStartLoadWith request: URLRequest, navigationType: UIWebView.NavigationType) -> Bool {
if let url = request.url, url.scheme == "oauth-swift" {
// Call here AppDelegate.sharedInstance.applicationHandleOpenURL(url) if necessary ie. if AppDelegate not configured to handle URL scheme
// compare the url with your own custom provided one in `authorizeWithCallbackURL`
self.dismissWebViewController()
}
return true
}
}

#elseif os(OSX)
extension WebViewController: WKNavigationDelegate {

func webView(_ webView: WKWebView, decidePolicyFor navigationAction: WKNavigationAction, decisionHandler: @escaping (WKNavigationActionPolicy) -> Void) {

// here we handle internally the callback url and call method that call handleOpenURL (not app scheme used)
if let url = navigationAction.request.url , url.scheme == "oauth-swift" {
AppDelegate.sharedInstance.applicationHandle(url: url)
decisionHandler(.cancel)

self.dismissWebViewController()
return
}
extension WebViewController: WKNavigationDelegate {

func webView(_ webView: WKWebView, decidePolicyFor navigationAction: WKNavigationAction, decisionHandler: @escaping (WKNavigationActionPolicy) -> Void) {

// here we handle internally the callback url and call method that call handleOpenURL (not app scheme used)
if let url = navigationAction.request.url , url.scheme == "oauth-swift" {
AppDelegate.sharedInstance.applicationHandle(url: url)
decisionHandler(.cancel)

decisionHandler(.allow)
self.dismissWebViewController()
return
}

/* override func webView(webView: WebView!, decidePolicyForNavigationAction actionInformation: [NSObject : AnyObject]!, request: URLRequest!, frame: WebFrame!, decisionListener listener: WebPolicyDecisionListener!) {

if request.URL?.scheme == "oauth-swift" {
decisionHandler(.allow)
}

func webView(_ webView: WKWebView, didFail navigation: WKNavigation!, withError error: Error) {
print("\(error)")
self.dismissWebViewController()
}

} */


func webView(_ webView: WKWebView, didFail navigation: WKNavigation!, withError error: Error) {
print("\(error)")
self.dismissWebViewController()
// maybe cancel request...
}
// maybe cancel request...
}
#endif
/* override func webView(webView: WebView!, decidePolicyForNavigationAction actionInformation: [NSObject : AnyObject]!, request: URLRequest!, frame: WebFrame!, decisionListener listener: WebPolicyDecisionListener!) {

if request.URL?.scheme == "oauth-swift" {
self.dismissWebViewController()
}

} */
}

0 comments on commit b40aebe

Please sign in to comment.