[GH-ISSUE #199] How to handle the user tapping Done on the SafariURLHandler? #117

Closed
opened 2026-03-03 16:45:49 +03:00 by kerem · 2 comments
Owner

Originally created by @dbmrq on GitHub (Feb 26, 2016).
Original GitHub issue: https://github.com/OAuthSwift/OAuthSwift/issues/199

I'm using SafariURLHandler and then I do authorizeWithCallbackURL… on success I show an alert saying the authorization succeeded and on failure I show an alert saying the authorization failed. But there is still the possibility that the user will tap "Done" without either authorizing or denying my app.

Then I guess I should still present the failure alert, but neither the success nor the failure closures get called in that case.

Another possibility would be to check if I got the token and, if I didn't, present the alert, but authorizeWithCallbackURL is an asynchronous function, so the alert would be presented before it's done.

I could also wait for the function to return, but it seems it just doesn't return in that case.

So for now it looks like my only option is to wait an arbitrary amount of time and then decide if I should present the alert or not, which isn't very nice. I think there should be a better way to handle this… maybe the failure closure could also be called when the user taps done without authorizing, or something like that.

Originally created by @dbmrq on GitHub (Feb 26, 2016). Original GitHub issue: https://github.com/OAuthSwift/OAuthSwift/issues/199 I'm using SafariURLHandler and then I do `authorizeWithCallbackURL`… on `success` I show an alert saying the authorization succeeded and on `failure` I show an alert saying the authorization failed. But there is still the possibility that the user will tap "Done" without either authorizing or denying my app. Then I guess I should still present the failure alert, but neither the `success` nor the `failure` closures get called in that case. Another possibility would be to check if I got the token and, if I didn't, present the alert, but `authorizeWithCallbackURL` is an asynchronous function, so the alert would be presented before it's done. I could also wait for the function to return, but it seems it just doesn't return in that case. So for now it looks like my only option is to wait an arbitrary amount of time and then decide if I should present the alert or not, which isn't very nice. I think there should be a better way to handle this… maybe the `failure` closure could also be called when the user taps done without authorizing, or something like that.
kerem closed this issue 2026-03-03 16:45:49 +03:00
Author
Owner

@dbmrq commented on GitHub (Feb 26, 2016):

Ok, I figured this out by myself.
All I had to do was set the SafariURLHandler delegate to my view controller and implement safariViewControllerDidFinish:

class GoodreadsSettingsTableViewController: UITableViewController, SFSafariViewControllerDelegate {

    func oAuth() {
        let safari = SafariURLHandler(viewController: self)
        safari.delegate = self
        oAuth.authorize_url_handler = safari
        oAuth.authorizeWithCallbackURL()
    }

    func safariViewControllerDidFinish(controller: SFSafariViewController) {
        if KeychainSwift().get("goodreadsToken") == nil {
            oAuthFailedAlert()
        }
    }
<!-- gh-comment-id:189521237 --> @dbmrq commented on GitHub (Feb 26, 2016): Ok, I figured this out by myself. All I had to do was set the `SafariURLHandler` delegate to my view controller and implement `safariViewControllerDidFinish`: ``` swift class GoodreadsSettingsTableViewController: UITableViewController, SFSafariViewControllerDelegate { func oAuth() { let safari = SafariURLHandler(viewController: self) safari.delegate = self oAuth.authorize_url_handler = safari oAuth.authorizeWithCallbackURL(…) } func safariViewControllerDidFinish(controller: SFSafariViewController) { if KeychainSwift().get("goodreadsToken") == nil { oAuthFailedAlert() } } ```
Author
Owner

@imkashrao commented on GitHub (Oct 22, 2016):

Another way of dealing it with setting handler.delegate = self and implement the SafariURLHandlerDelegate methods. However, you will have to conform SFSafariViewControllerDelegate in your ViewController.

<!-- gh-comment-id:255559290 --> @imkashrao commented on GitHub (Oct 22, 2016): Another way of dealing it with setting handler.delegate = self and implement the SafariURLHandlerDelegate methods. However, you will have to conform SFSafariViewControllerDelegate in your ViewController.
Sign in to join this conversation.
No milestone
No project
No assignees
1 participant
Notifications
Due date
The due date is invalid or out of range. Please use the format "yyyy-mm-dd".

No due date set.

Dependencies

No dependencies set.

Reference
starred/OAuthSwift#117
No description provided.