From 09c120d137920d4342aa7de07349472bc28373d2 Mon Sep 17 00:00:00 2001 From: Sahil Mahajan Date: Mon, 29 May 2017 18:45:23 +0530 Subject: [PATCH 1/2] Handle done button action --- Simplicity/Simplicity.swift | 11 +++++++++-- 1 file changed, 9 insertions(+), 2 deletions(-) diff --git a/Simplicity/Simplicity.swift b/Simplicity/Simplicity.swift index 347a888..e3f6024 100644 --- a/Simplicity/Simplicity.swift +++ b/Simplicity/Simplicity.swift @@ -15,11 +15,12 @@ public typealias ExternalLoginCallback = (String?, NSError?) -> Void /** Simplicity is a framework for authenticating with external providers on iOS. */ -public final class Simplicity { +public final class Simplicity: NSObject, SFSafariViewControllerDelegate { private static var currentLoginProvider: LoginProvider? private static var callback: ExternalLoginCallback? private static var safari: UIViewController? - + private static let instance = Simplicity () + /** Begin the login flow by redirecting to the LoginProvider's website. @@ -54,6 +55,7 @@ public final class Simplicity { private static func presentSafariView(_ url: URL) { if #available(iOS 9, *) { safari = SFSafariViewController(url: url) + (safari as! SFSafariViewController).delegate = instance var topController = UIApplication.shared.keyWindow?.rootViewController while let vc = topController?.presentedViewController { topController = vc @@ -63,4 +65,9 @@ public final class Simplicity { UIApplication.shared.openURL(url) } } + + @available(iOS 9.0, *) + public func safariViewControllerDidFinish(_ controller: SFSafariViewController) { + Simplicity.callback!(nil, LoginError.InternalSDKError) + } } From f70dfc2442b12e2366afe60406140114e3673e44 Mon Sep 17 00:00:00 2001 From: Sahil Mahajan Date: Thu, 15 Jun 2017 01:35:45 +0530 Subject: [PATCH 2/2] Fix Issue: When User cancel the login process, no callback return to the application. --- Simplicity/LoginError.swift | 3 +++ Simplicity/Simplicity.swift | 11 ++++++++--- 2 files changed, 11 insertions(+), 3 deletions(-) diff --git a/Simplicity/LoginError.swift b/Simplicity/LoginError.swift index c8cf29a..3d0c950 100644 --- a/Simplicity/LoginError.swift +++ b/Simplicity/LoginError.swift @@ -16,6 +16,9 @@ public class LoginError: NSError { /// An error that should never happen. If seen, please open a GitHub issue. public static let InternalSDKError = LoginError(code: 0, description: "Internal SDK Error") + /// An error if user cancel the SafariViewController. + public static let LoginCancelledError = LoginError(code: 1, description: "Login Cancelled by user") + /** Initializer for LoginError diff --git a/Simplicity/Simplicity.swift b/Simplicity/Simplicity.swift index e3f6024..917a9fb 100644 --- a/Simplicity/Simplicity.swift +++ b/Simplicity/Simplicity.swift @@ -19,7 +19,7 @@ public final class Simplicity: NSObject, SFSafariViewControllerDelegate { private static var currentLoginProvider: LoginProvider? private static var callback: ExternalLoginCallback? private static var safari: UIViewController? - private static let instance = Simplicity () + private static let instance = Simplicity() /** Begin the login flow by redirecting to the LoginProvider's website. @@ -55,11 +55,13 @@ public final class Simplicity: NSObject, SFSafariViewControllerDelegate { private static func presentSafariView(_ url: URL) { if #available(iOS 9, *) { safari = SFSafariViewController(url: url) - (safari as! SFSafariViewController).delegate = instance var topController = UIApplication.shared.keyWindow?.rootViewController while let vc = topController?.presentedViewController { topController = vc } + if let safari = safari as? SFSafariViewController { + safari.delegate = instance + } topController?.present(safari!, animated: true, completion: nil) } else { UIApplication.shared.openURL(url) @@ -68,6 +70,9 @@ public final class Simplicity: NSObject, SFSafariViewControllerDelegate { @available(iOS 9.0, *) public func safariViewControllerDidFinish(_ controller: SFSafariViewController) { - Simplicity.callback!(nil, LoginError.InternalSDKError) + guard let callback = Simplicity.callback else { + return + } + callback(nil, LoginError.LoginCancelledError) } }