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.

2

1 Answer 1

0

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

let escapedString = originalString.addingPercentEncoding(withAllowedCharacters: .urlHostAllowed)
Sign up to request clarification or add additional context in comments.

1 Comment

There are some other possibilities here: stackoverflow.com/questions/24551816/swift-encode-url

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.