Skip to content

Commit e8dd8e4

Browse files
committed
Make error optional on the onFailure closure to indicate cancelled state
Also use `onWillDismiss` in the iOS web view controller to indicate that the closure will be called analog to a "viewWillDisappear" message. There currently is no "onDidDismiss" closure.
1 parent baf1a0b commit e8dd8e4

File tree

4 files changed

+16
-18
lines changed

4 files changed

+16
-18
lines changed

OAuth2/OAuth2.swift

+3-8
Original file line numberDiff line numberDiff line change
@@ -56,8 +56,8 @@ public class OAuth2 {
5656
/** Closure called on successful authentication. */
5757
public var onAuthorize: ((parameters: NSDictionary) -> Void)?
5858

59-
/** When authorization fails. */
60-
public var onFailure: ((error: NSError) -> Void)?
59+
/** When authorization fails (if error is not nil) or is cancelled. */
60+
public var onFailure: ((error: NSError?) -> Void)?
6161

6262
/** Closure called after onAuthorize OR onFailure, useful for cleanup operations. */
6363
public var afterAuthorizeOrFailure: ((wasFailure: Bool) -> Void)?
@@ -204,12 +204,7 @@ public class OAuth2 {
204204
}
205205

206206
func didFail(error: NSError?) {
207-
if nil == error {
208-
onFailure?(error: genOAuth2Error("I failed, sorry"))
209-
}
210-
else {
211-
onFailure?(error: error!)
212-
}
207+
onFailure?(error: error)
213208
afterAuthorizeOrFailure?(wasFailure: true)
214209
}
215210

OAuth2/OAuth2WebViewController.swift

+6-7
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,7 @@ extension OAuth2 {
3333
self.handleRedirectURL(url)
3434
return true
3535
}
36-
web.onDismiss = { didCancel in
36+
web.onWillDismiss = { didCancel in
3737
if didCancel {
3838
self.didFail(nil)
3939
}
@@ -74,8 +74,8 @@ public class OAuth2WebViewController: UIViewController, UIWebViewDelegate
7474
/** Closure called when the web view gets asked to load the redirect URL, specified in `interceptURLString`. */
7575
var onIntercept: ((url: NSURL) -> Bool)?
7676

77-
/** Called when the web view gets dismissed. */
78-
var onDismiss: ((didCancel: Bool) -> Void)?
77+
/** Called when the web view is about to be dismissed. */
78+
var onWillDismiss: ((didCancel: Bool) -> Void)?
7979

8080
var cancelButton: UIBarButtonItem?
8181
var webView: UIWebView!
@@ -174,11 +174,10 @@ public class OAuth2WebViewController: UIViewController, UIWebViewDelegate
174174
func dismiss(# asCancel: Bool, animated: Bool) {
175175
webView.stopLoading()
176176

177-
presentingViewController?.dismissViewControllerAnimated(animated) {
178-
if nil != self.onDismiss {
179-
self.onDismiss!(didCancel: asCancel)
180-
}
177+
if nil != self.onWillDismiss {
178+
self.onWillDismiss!(didCancel: asCancel)
181179
}
180+
presentingViewController?.dismissViewControllerAnimated(animated, nil)
182181
}
183182

184183

OSX.playground/section-1.swift

+3-1
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,9 @@ oauth.onAuthorize = { parameters in
3636
task.resume()
3737
}
3838
oauth.onFailure = { error in
39-
println("Authorization failed: \(error.localizedDescription)")
39+
if nil != error {
40+
println("Authorization failed: \(error!.localizedDescription)")
41+
}
4042
}
4143

4244
// construct the authorize URL which you would then load in a browser or web view

README.md

+4-2
Original file line numberDiff line numberDiff line change
@@ -32,8 +32,10 @@ The steps for other flows are mostly the same short of instantiating a different
3232
oauth.onAuthorize = { parameters in
3333
println("Did authorize with parameters: \(parameters)")
3434
}
35-
oauth.onFailure = { error in
36-
println("Authorization went wrong: \(error.localizedDescription)")
35+
oauth.onFailure = { error in // `error` is nil on cancel
36+
if nil != error {
37+
println("Authorization went wrong: \(error!.localizedDescription)")
38+
}
3739
}
3840
```
3941

0 commit comments

Comments
 (0)