Skip to content

Commit

Permalink
Merge pull request pichillilorenzo#8 from jj-lin/iOS-Google-login
Browse files Browse the repository at this point in the history
iOS Google Login
  • Loading branch information
eJamesLin authored and GitHub Enterprise committed Jun 10, 2020
2 parents add05c6 + a0a4ec3 commit 00594ae
Showing 1 changed file with 63 additions and 14 deletions.
77 changes: 63 additions & 14 deletions ios/Classes/InAppWebView.swift
Original file line number Diff line number Diff line change
Expand Up @@ -1505,9 +1505,18 @@ public class InAppWebView: WKWebView, UIScrollViewDelegate, WKUIDelegate, WKNavi
if let url = navigationAction.request.url {

if activateShouldOverrideUrlLoading && (options?.useShouldOverrideUrlLoading)! {

let isForMainFrame = navigationAction.targetFrame?.isMainFrame ?? false

let socialLogin = SocialLogin(url: url)
if case .google = socialLogin {
decisionHandler(.cancel)
_ = createSocialLoginWindow(from: webView,
createWebViewWith: configuration,
urlRequest: navigationAction.request,
socialLogin: socialLogin)
return
}

shouldOverrideUrlLoading(url: url, method: navigationAction.request.httpMethod, headers: navigationAction.request.allHTTPHeaderFields, isForMainFrame: isForMainFrame, navigationType: navigationAction.navigationType, result: { (result) -> Void in
if result is FlutterError {
print((result as! FlutterError).message ?? "")
Expand Down Expand Up @@ -2068,9 +2077,44 @@ public class InAppWebView: WKWebView, UIScrollViewDelegate, WKUIDelegate, WKNavi

var popupWebView : WKWebView?

// sample url: https://m.facebook.com/v2.8/dialog/oauth?app_id=1091264071008011....
func isFacebookLogin(url: URL) -> Bool {
return url.host?.contains("facebook") == true && url.path.contains("oauth")
enum SocialLogin {
case facebook
case google
case none

// sample url: https://m.facebook.com/v2.8/dialog/oauth?app_id=1091264071008011....
static func isFacebookLogin(url: URL) -> Bool {
return url.host?.contains("facebook") == true && url.path.contains("/oauth")
}

// sample url: https://accounts.google.com/o/oauth2/auth?redirect_uri=storagerelay%3A%2F%2Fhtt
static func isGoogleLogin(url: URL) -> Bool {
return url.host?.contains("google") == true && url.path.contains("/auth")
}

init(url: URL) {
switch url {
case _ where SocialLogin.isFacebookLogin(url: url): self = .facebook
case _ where SocialLogin.isGoogleLogin(url: url): self = .google
default: self = .none
}
}
}

func createSocialLoginWindow(from webView: WKWebView,
createWebViewWith configuration: WKWebViewConfiguration,
urlRequest: URLRequest,
socialLogin: SocialLogin) -> WKWebView {
let newWebView = WKWebView(frame: webView.frame, configuration: configuration)
if #available(iOS 9.0, *), case .google = socialLogin {
// Avoid google 403 disallowed_useragent issue
newWebView.customUserAgent = "Mozilla/5.0 (Macintosh) AppleWebKit/605.1.15 (KHTML, like Gecko)"
}
newWebView.uiDelegate = self
webView.superview?.addSubview(newWebView)
newWebView.load(urlRequest)
popupWebView = newWebView
return newWebView
}

public func webView(_ webView: WKWebView,
Expand All @@ -2079,17 +2123,22 @@ public class InAppWebView: WKWebView, UIScrollViewDelegate, WKUIDelegate, WKNavi
windowFeatures: WKWindowFeatures) -> WKWebView? {
guard let url = navigationAction.request.url else { return nil }

if navigationAction.targetFrame?.isMainFrame != true && isFacebookLogin(url: url) {
let newWebView = WKWebView(frame: webView.frame, configuration: configuration)
newWebView.load(navigationAction.request)
newWebView.uiDelegate = self
webView.superview?.addSubview(newWebView)
popupWebView = newWebView
return newWebView
if navigationAction.targetFrame?.isMainFrame == true {
onCreateWindow(url: url, navigationType: navigationAction.navigationType)
return nil
}

onCreateWindow(url: url, navigationType: navigationAction.navigationType)
return nil
let socialLogin = SocialLogin(url: url)
switch socialLogin {
case .none:
onCreateWindow(url: url, navigationType: navigationAction.navigationType)
return nil
case .facebook, .google:
return createSocialLoginWindow(from: webView,
createWebViewWith: configuration,
urlRequest: navigationAction.request,
socialLogin: socialLogin)
}
}

public func webViewDidClose(_ webView: WKWebView) {
Expand Down

0 comments on commit 00594ae

Please sign in to comment.