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 347a888..917a9fb 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. @@ -58,9 +59,20 @@ public final class Simplicity { 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) } } + + @available(iOS 9.0, *) + public func safariViewControllerDidFinish(_ controller: SFSafariViewController) { + guard let callback = Simplicity.callback else { + return + } + callback(nil, LoginError.LoginCancelledError) + } }