0

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

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:

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.

    <array>
        <dict>
            <key>CFBundleTypeRole</key>
            <string>Editor</string>
            <key>CFBundleURLName</key>
            <string>com.xxx.yyy</string>
            <key>CFBundleURLSchemes</key>
            <array>
                <string>myapp</string>
            </array>
        </dict>
    </array>

Below is the code to execute this simple URLSession data task

        let s = URLSession.shared
        let u = URL(string: "https://xxx.yyy.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")!        
        
        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

let u = URL(string: "https://xxx.yyy.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")!        

let u = URL(string: "https://xxx.yyy.auth.us-east-1.amazoncognito.com/logout?client_id=49jaf4a848hakh&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?

Update After some more experimentation, what’s really weird is that there is no issue if I only have 1 query string parameter with a scheme as a value. Seems like a bug that it doesn’t support more than 1 query string parameter with a scheme as a value.

Bumbleparrot
  • 323
  • 1
  • 8
  • Have you tried to remove the percent-encoding? https://developer.apple.com/documentation/foundation/nsstring/1409569-removingpercentencoding Can you paste your URLSession code here please? – Canh Tran Aug 03 '20 at 05:22
  • Updated with the URLSession code. – Bumbleparrot Aug 03 '20 at 17:57

1 Answers1

0

This seems like an encoding issue. Have you tried encoding like this:

let escapedString = originalString.addingPercentEncoding(withAllowedCharacters: .urlHostAllowed)
Ryan Jones
  • 236
  • 2
  • 5