[GH-ISSUE #664] App Crash when using ASWebAuthenticationSession in iOS 14.5 #432

Open
opened 2026-03-03 16:48:38 +03:00 by kerem · 9 comments
Owner

Originally created by @RahulTak on GitHub (Jun 10, 2021).
Original GitHub issue: https://github.com/OAuthSwift/OAuthSwift/issues/664

Description:

File: ASWebAuthenticationURLHandler.swift
My app is crash when I am using the

ASWebAuthenticationSession(
            url: url,
            callbackURLScheme: callbackUrlScheme,
            completionHandler: { callback, error in
                if let error = error {
                    let msg = error.localizedDescription.addingPercentEncoding(withAllowedCharacters: .urlHostAllowed)
                    let errorDomain = (error as NSError).domain
                    let errorCode = (error as NSError).code
                    let urlString = "\(self.callbackUrlScheme):?error=\(msg ?? "UNKNOWN")&error_domain=\(errorDomain)&error_code=\(errorCode)"
                    let url = URL(string: urlString)!
                    #if !OAUTH_APP_EXTENSIONS
                    UIApplication.shared.open(url, options: [:], completionHandler: nil)
                    #endif
                } else if let successURL = callback {
                    #if !OAUTH_APP_EXTENSIONS
                    UIApplication.shared.open(successURL, options: [:], completionHandler: nil)
                    #endif
                }
        })

Then Crash detail is

*** Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: 'The provided scheme is not valid. 
A scheme should not include special characters such as ":" or "/".'

if I replace callbackUrlScheme with callbackUrlScheme .addingPercentEncoding(withAllowedCharacters: .urlHostAllowed) it's working fine.

So please let me know how to add this lines into your package and use into my project.
Or should I have to create the new branch for this is send you the PR for merge into this?

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

OAuth Version:

  • Version 1
  • Version 2

OS (Please fill the version) :

  • iOS : 14.5
  • 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:

  • 12.5 (Swift 5.0)

  • 11.4 (Swift 5.2)

  • 11.x (Swift 5.1)

  • 10.x (Swift 5.0)

  • other: (Please fill in the version you are using.)

  • objective c

Originally created by @RahulTak on GitHub (Jun 10, 2021). Original GitHub issue: https://github.com/OAuthSwift/OAuthSwift/issues/664 ### Description: File: `ASWebAuthenticationURLHandler.swift` My app is crash when I am using the ``` ASWebAuthenticationSession( url: url, callbackURLScheme: callbackUrlScheme, completionHandler: { callback, error in if let error = error { let msg = error.localizedDescription.addingPercentEncoding(withAllowedCharacters: .urlHostAllowed) let errorDomain = (error as NSError).domain let errorCode = (error as NSError).code let urlString = "\(self.callbackUrlScheme):?error=\(msg ?? "UNKNOWN")&error_domain=\(errorDomain)&error_code=\(errorCode)" let url = URL(string: urlString)! #if !OAUTH_APP_EXTENSIONS UIApplication.shared.open(url, options: [:], completionHandler: nil) #endif } else if let successURL = callback { #if !OAUTH_APP_EXTENSIONS UIApplication.shared.open(successURL, options: [:], completionHandler: nil) #endif } }) ``` Then Crash detail is ``` *** Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: 'The provided scheme is not valid. A scheme should not include special characters such as ":" or "/".' ``` if I replace `callbackUrlScheme` with `callbackUrlScheme .addingPercentEncoding(withAllowedCharacters: .urlHostAllowed)` it's working fine. So please let me know how to add this lines into your package and use into my project. Or should I have to create the new branch for this is send you the PR for merge into this? ### OAuth Provider? (Twitter, Github, ..): ### OAuth Version: - [ ] Version 1 - [x] Version 2 ### OS (Please fill the version) : - [x] iOS : 14.5 - [ ] OSX : - [ ] TVOS : - [ ] WatchOS : ### Installation method: - [ ] Carthage - [ ] CocoaPods - [x] Swift Package Manager - [ ] Manually ### Library version: - [x] head - [x] v2.1.0 - [ ] v2.0.0 - [ ] v1.4.1 - [ ] other: (Please fill in the version you are using.) ### Xcode version: - [x] 12.5 (Swift 5.0) - [ ] 11.4 (Swift 5.2) - [ ] 11.x (Swift 5.1) - [ ] 10.x (Swift 5.0) - [ ] other: (Please fill in the version you are using.) - [ ] objective c
Author
Owner

@phimage commented on GitHub (Jun 10, 2021):

you could show code with link
github.com/OAuthSwift/OAuthSwift@dbb74fa871/Sources/Handler/ASWebAuthenticationURLHandler.swift (L38)

first yes ! forecast must be forbidden in OAuthSwift code

I see some edit about that recently, maybe it depend on swift version

<!-- gh-comment-id:858780429 --> @phimage commented on GitHub (Jun 10, 2021): you could show code with link https://github.com/OAuthSwift/OAuthSwift/blob/dbb74fa8717843017fc54b3584f716ccd8ba3e7c/Sources/Handler/ASWebAuthenticationURLHandler.swift#L38 first yes `!` forecast must be forbidden in OAuthSwift code I see some edit about that recently, maybe it depend on swift version
Author
Owner

@RahulTak commented on GitHub (Jun 10, 2021):

@phimage Yes forecast must be forbidden,
but my point is different That app crashing while creating the object of ASWebAuthenticationSession because in this we are sending callbackURLScheme normally.

But we required to send like this callbackUrlScheme.addingPercentEncoding(withAllowedCharacters: .urlHostAllowed) I have tested this with my by cloning your package and made this changes

<!-- gh-comment-id:858790954 --> @RahulTak commented on GitHub (Jun 10, 2021): @phimage Yes forecast must be forbidden, but my point is different That app crashing while creating the object of `ASWebAuthenticationSession` because in this we are sending `callbackURLScheme` normally. But we required to send like this `callbackUrlScheme.addingPercentEncoding(withAllowedCharacters: .urlHostAllowed)` I have tested this with my by cloning your package and made this changes
Author
Owner

@phimage commented on GitHub (Jun 10, 2021):

your value of callbackURLScheme is ?

because you pass it , so you could encode it if very necessary (or not is based to ASWebAuthenticationSession is different)
if you put ":" or "/" in a scheme it's your error maybe?

<!-- gh-comment-id:858837337 --> @phimage commented on GitHub (Jun 10, 2021): your value of `callbackURLScheme` is ? because you pass it , so you could encode it if very necessary (or not is based to ASWebAuthenticationSession is different) if you put ":" or "/" in a scheme it's your error maybe?
Author
Owner

@RahulTak commented on GitHub (Jun 10, 2021):

@phimage yes this is the problem, can you please help me to resolve this ?

<!-- gh-comment-id:858849839 --> @RahulTak commented on GitHub (Jun 10, 2021): @phimage yes this is the problem, can you please help me to resolve this ?
Author
Owner

@phatblat commented on GitHub (Jun 10, 2021):

@RahulTak are you passing a full URL as the callbackScheme? Your scheme should not contain a colon. ASWebAuthenticationSession used to only log an error if you passed a full URL as the scheme but as of 14.5 it crashes

<!-- gh-comment-id:858923169 --> @phatblat commented on GitHub (Jun 10, 2021): @RahulTak are you passing a full URL as the callbackScheme? Your scheme should not contain a colon. ASWebAuthenticationSession used to only log an error if you passed a full URL as the scheme but as of 14.5 it crashes
Author
Owner

@RahulTak commented on GitHub (Jun 10, 2021):

@phimage I am passing the URL like myappSchemeName://authorizedcallback because we need this while popup dismiss.

<!-- gh-comment-id:858987646 --> @RahulTak commented on GitHub (Jun 10, 2021): @phimage I am passing the URL like `myappSchemeName://authorizedcallback` because we need this while popup dismiss.
Author
Owner

@phimage commented on GitHub (Jun 10, 2021):

that's not a scheme.

an url isscheme://host/path?query#fragment

<!-- gh-comment-id:859024679 --> @phimage commented on GitHub (Jun 10, 2021): that's not a scheme. an url is`scheme://host/path?query#fragment`
Author
Owner

@RahulTak commented on GitHub (Jun 10, 2021):

But myappSchemeName://authorizedcallback this is working fine into the iOS 14.4

<!-- gh-comment-id:859043068 --> @RahulTak commented on GitHub (Jun 10, 2021): But `myappSchemeName://authorizedcallback ` this is working fine into the iOS 14.4
Author
Owner

@phatblat commented on GitHub (Jun 10, 2021):

Yep, it worked for us too in 14.4, but check your logs for the error. What changed in 14.5 is that Apple is now crashing instead of just logging an error.

<!-- gh-comment-id:859068489 --> @phatblat commented on GitHub (Jun 10, 2021): Yep, it worked for us too in 14.4, but check your logs for the error. What changed in 14.5 is that Apple is now crashing instead of just logging an error.
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#432
No description provided.