Swift URL with Custom Scheme in Query String Fails

I'm trying to call a URL using URLSession to log out of an oauth2 session in swift in an iOS app.

The URL is something like this:
  • Notice that some parameters are URL Encoded*

Code Block
https://xxxxxx.auth.us-east-1.amazoncognito.com/logout?client_id=49jaf4a848hakh&logout_uri=myapp%3A%2F%2F&redirect_uri=myapp%3A%2F%2F&response_type=token


  • I get a run time error using this URL in a data task that says:*

Code Block
Error Domain=NSURLErrorDomain Code=-1002 "unsupported URL"
UserInfo={NSUnderlyingError=0x60000202ccf0
{Error Domain=kCFErrorDomainCFNetwork Code=-1002 "(null)"},
NSErrorFailingURLStringKey=myapp://, NSErrorFailingURLKey=myapp://,
NSLocalizedDescription=unsupported URL}

  • I already have "myapp" in my info.plist.*

Code Block
<array>
<dict>
<key>CFBundleTypeRole</key>
<string>Editor</string>
<key>CFBundleURLName</key>
<string>com.***.yyy</string>
<key>CFBundleURLSchemes</key>
<array>
<string>myapp</string>
</array>
</dict>
</array>

  • Below is the code to execute this simple URLSession data task*

Code Block
let s = URLSession.shared
let u = URL(string: "https://***.yyy.auth.us-east-1.amazoncognito.com/logout?client_id=7c2aqa0fibv51v8valq6pagtoq&logout_uri=myapp%3A%2F%2F&redirect_uri=myapp%3A%2F%2F&response_type=token")!
let t = s.dataTask(with: u) { (d: Data?, r: URLResponse?, e:Error?) in
if e != nil {
print("error: \(e)")
}
let decoded = String(data: d!, encoding: .utf8)
print("Data: \(decoded)")
print("url Response: \(r)")
print("Breakpoint")
}.resume()

  • I've tried with both URL strings below and still get the same error*

Code Block
let u = URL(string: "https://***.yyy.auth.us-east-1.amazoncognito.com/logout?client_id=7c2aqa0fibv51v8valq6pagtoq&logout_uri=myapp%3A%2F%2F&redirect_uri=myapp%3A%2F%2F&response_type=token")!
let u = URL(string: "https://***.yyy.auth.us-east-1.amazoncognito.com/logout?client_id=7c2aqa0fibv51v8valq6pagtoq&logout_uri=myapp://&redirect_uri=myapp://&response_type=token")!


Why doesn't swift like "myapp://" or (myapp%3A%2F%2F) being a query string parameter?

Stack Overflow Question
I think what’s happening here is that the server is redirecting the request to your myapp:// URL and URLSession has no idea how to follow that redirection. Adding that scheme to your Info.plist won’t help because that’s only consulted when you open the URL using things like -[UIApplication openURL:].

What you should do here is create your own URLSession, set up the delegate, and catch the redirect in urlSession(_:task:willPerformHTTPRedirection:newRequest:completionHandler:).

Share and Enjoy

Quinn “The Eskimo!” @ Developer Technical Support @ Apple
let myEmail = "eskimo" + "1" + "@apple.com"
You know what's really weird?

If I take out one of the for "myapp://" query string parameters it works without any issue.

This only fails when there are TWO query parameters in the URL that contain a "scheme".

The URL in general is valid and works if you put it in a browser.
  • What the heck! Seems like a bug to me!*

The URL in general is valid and works if you put it in a browser.

URLSession is not a web browser. It will not follow redirects by opening a custom URL using -[UIApplication openURL:] and friends.

Based on the info you’ve posted so far it’s impossible to say for sure what’s going on. You need to implement the redirect handler and see how the server is responding to the various URLs you send it.

Or you could look at CFNetwork diagnostic logging.

Share and Enjoy

Quinn “The Eskimo!” @ Developer Technical Support @ Apple
let myEmail = "eskimo" + "1" + "@apple.com"
Can you take another look?

I don't think the URL() object actually makes any requests. It just regex's and validates the URL locally right?

Why does it work with 1 query string parameter that contains a custom scheme "myapp%3A%2F%2F (myapp://), but it doesn't work if i have two parameters with the same scheme as a value?

Can you take another look?

I’m going to recommend that you open a DTS tech support incident so that I, or one of my colleagues, can take an in-depth look at this.

Share and Enjoy

Quinn “The Eskimo!” @ Developer Technical Support @ Apple
let myEmail = "eskimo" + "1" + "@apple.com"
Swift URL with Custom Scheme in Query String Fails
 
 
Q