Skip to content
Open
Show file tree
Hide file tree
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
3 changes: 3 additions & 0 deletions Simplicity/LoginError.swift
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down
16 changes: 14 additions & 2 deletions Simplicity/Simplicity.swift
Original file line number Diff line number Diff line change
Expand Up @@ -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.

Expand Down Expand Up @@ -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)
}
}