[GH-ISSUE #632] Retain error -10 only when assigning authorizeURLHandler? #413

Closed
opened 2026-03-03 16:48:29 +03:00 by kerem · 3 comments
Owner

Originally created by @serenapascual on GitHub (Nov 9, 2020).
Original GitHub issue: https://github.com/OAuthSwift/OAuthSwift/issues/632

Description:

When I do not set authorizeURLHandler and allow the URL to open in an external browser, the authorization is successful. However, once I try to use SafariURLHandler, I get a retain error:
The operation couldn’t be completed. (OAuthSwiftError error -10.)

Here is my code:

class AccountViewController: UIViewController {
	
    var oauthswift: OAuth2Swift!
	
    override func viewDidLoad() {
        super.viewDidLoad()

		let loginButton: UIButton = {
                    // . . . 
                }		
	}
    
	@objc func loginButtonAction(_ sender:UIButton!) {
		doOAuthReddit()
	}
	
}

extension AccountViewController {
	
	func doOAuthReddit() {
		self.oauthswift = OAuth2Swift(
			consumerKey: "",
			consumerSecret: "",
			authorizeUrl: "https://www.reddit.com/api/v1/authorize.compact",
			accessTokenUrl: "https://www.reddit.com/api/v1/access_token",
			responseType: "code"
		)
				
		oauthswift.accessTokenBasicAuthentification = true

                // Success if the line below is commented out 
		oauthswift.authorizeURLHandler = SafariURLHandler(viewController: self, oauthSwift: oauthswift)
		
		let _ = oauthswift.authorize(withCallbackURL: "oauth://oauth-callback", scope: "read", state: "aStateOfFearAndConfusionOhDear") { result in
				switch result {
				case .success(let (credential, _, _)):
					print(credential.oauthToken)
				case .failure(let error):
					print(error.localizedDescription)
				}
		}
	}
	
}

I have also tried creating a property for the SafariURLHandler instance and storing that there, but doing so did not help.

OAuth Provider? (Twitter, Github, ..):

Reddit

OAuth Version:

  • Version 1
  • Version 2

OS (Please fill the version) :

  • iOS : 14.1
  • OSX :
  • TVOS :
  • WatchOS :

Installation method:

  • Carthage
  • CocoaPods
  • Swift Package Manager
  • Manually

Library version:

  • head
  • v2.1.0
  • v2.0.0
  • v1.4.1
  • other: (Please fill in the version you are using.)

Xcode version:

  • 11.4 (Swift 5.2)

  • 11.x (Swift 5.1)

  • 10.x (Swift 5.0)

  • other: 12.1 (Swift 5.3)

  • objective c

Originally created by @serenapascual on GitHub (Nov 9, 2020). Original GitHub issue: https://github.com/OAuthSwift/OAuthSwift/issues/632 ### Description: When I do not set authorizeURLHandler and allow the URL to open in an external browser, the authorization is successful. However, once I try to use SafariURLHandler, I get a retain error: `The operation couldn’t be completed. (OAuthSwiftError error -10.)` Here is my code: ``` class AccountViewController: UIViewController { var oauthswift: OAuth2Swift! override func viewDidLoad() { super.viewDidLoad() let loginButton: UIButton = { // . . . } } @objc func loginButtonAction(_ sender:UIButton!) { doOAuthReddit() } } extension AccountViewController { func doOAuthReddit() { self.oauthswift = OAuth2Swift( consumerKey: "", consumerSecret: "", authorizeUrl: "https://www.reddit.com/api/v1/authorize.compact", accessTokenUrl: "https://www.reddit.com/api/v1/access_token", responseType: "code" ) oauthswift.accessTokenBasicAuthentification = true // Success if the line below is commented out oauthswift.authorizeURLHandler = SafariURLHandler(viewController: self, oauthSwift: oauthswift) let _ = oauthswift.authorize(withCallbackURL: "oauth://oauth-callback", scope: "read", state: "aStateOfFearAndConfusionOhDear") { result in switch result { case .success(let (credential, _, _)): print(credential.oauthToken) case .failure(let error): print(error.localizedDescription) } } } } ``` I have also tried creating a property for the SafariURLHandler instance and storing that there, but doing so did not help. ### OAuth Provider? (Twitter, Github, ..): Reddit ### OAuth Version: - [ ] Version 1 - [x] Version 2 ### OS (Please fill the version) : - [x] iOS : 14.1 - [ ] OSX : - [ ] TVOS : - [ ] WatchOS : ### Installation method: - [ ] Carthage - [ ] CocoaPods - [ ] Swift Package Manager - [x] Manually ### Library version: - [x] head - [ ] v2.1.0 - [ ] v2.0.0 - [ ] v1.4.1 - [ ] other: (Please fill in the version you are using.) ### Xcode version: - [ ] 11.4 (Swift 5.2) - [ ] 11.x (Swift 5.1) - [ ] 10.x (Swift 5.0) - [x] other: 12.1 (Swift 5.3) - [ ] objective c
kerem closed this issue 2026-03-03 16:48:29 +03:00
Author
Owner

@phimage commented on GitHub (Nov 10, 2020):

it's seems weird
do you have dismissed AccountViewController ?

if not we will try to force "retain"

could you try to assign to a class variable the return of authorise instead of let _ =
or reference self in callback, for instance by extracting it to afunction

func log() {
                       switch result {
				case .success(let (credential, _, _)):
					print(credential.oauthToken)
				case .failure(let error):
					print(error.localizedDescription)
			}
}

and call self.log()in callback (yes it is crappy because you must use [weak self] to not retain ad cause memory leak)

<!-- gh-comment-id:724435865 --> @phimage commented on GitHub (Nov 10, 2020): it's seems weird do you have dismissed AccountViewController ? if not we will try to force "retain" could you try to assign to a class variable the return of authorise instead of `let _ = ` or reference self in callback, for instance by extracting it to afunction ```swift func log() { switch result { case .success(let (credential, _, _)): print(credential.oauthToken) case .failure(let error): print(error.localizedDescription) } } ``` and call` self.log()`in callback (yes it is crappy because you must use [weak self] to not retain ad cause memory leak)
Author
Owner

@serenapascual commented on GitHub (Nov 12, 2020):

Hi Eric, thank you for your prompt and helpful response.

do you have dismissed AccountViewController ?

No dismissal.

could you try to assign to a class variable the return of authorise instead of let _ =

I tried this and the same error occurs.

or reference self in callback, for instance by extracting it to afunction

func log() {
                       switch result {
				case .success(let (credential, _, _)):
					print(credential.oauthToken)
				case .failure(let error):
					print(error.localizedDescription)
			}
}

and call self.log()in callback (yes it is crappy because you must use [weak self] to not retain ad cause memory leak)

It took me a while to figure this out (sorry - new to Swift), and I think it works! At least, that is when I don't use [weak self], which causes self to evaluate to nil during callback. Do you have any tips?

This is what I've added that leads to a successful result.

var log: ((Result<OAuthSwift.TokenSuccess, OAuthSwiftError>) -> Void) = { result in
		switch result {
		 case .success(let (credential, _, _)):
			 print(credential.oauthToken)
		 case .failure(let error):
			 print(error.localizedDescription)
		}
}

// . . .

let _ = oauthswift.authorize(withCallbackURL: "oauth://oauth-callback", scope: "read", state: "aStateOfFearAndConfusionOhDear") { result in
			self.log(result)
}
<!-- gh-comment-id:725773329 --> @serenapascual commented on GitHub (Nov 12, 2020): Hi Eric, thank you for your prompt and helpful response. > do you have dismissed AccountViewController ? No dismissal. > could you try to assign to a class variable the return of authorise instead of `let _ = ` I tried this and the same error occurs. > or reference self in callback, for instance by extracting it to afunction > > ```swift > func log() { > switch result { > case .success(let (credential, _, _)): > print(credential.oauthToken) > case .failure(let error): > print(error.localizedDescription) > } > } > ``` > > and call` self.log()`in callback (yes it is crappy because you must use [weak self] to not retain ad cause memory leak) It took me a while to figure this out (sorry - new to Swift), and I think it works! At least, that is when I don't use [weak self], which causes self to evaluate to nil during callback. Do you have any tips? This is what I've added that leads to a successful result. ```swift var log: ((Result<OAuthSwift.TokenSuccess, OAuthSwiftError>) -> Void) = { result in switch result { case .success(let (credential, _, _)): print(credential.oauthToken) case .failure(let error): print(error.localizedDescription) } } // . . . let _ = oauthswift.authorize(withCallbackURL: "oauth://oauth-callback", scope: "read", state: "aStateOfFearAndConfusionOhDear") { result in self.log(result) } ```
Author
Owner

@phimage commented on GitHub (Apr 29, 2021):

Close Stale Issue

<!-- gh-comment-id:829318702 --> @phimage commented on GitHub (Apr 29, 2021): Close Stale Issue
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#413
No description provided.