【问题标题】:Swift URL with Custom Scheme in Query String Fails - Redirect_URI在查询字符串中使用自定义方案的 Swift URL 失败 - Redirect_URI
【发布时间】:2020-08-03 05:11:39
【问题描述】:

我正在尝试使用 URLSession 调用 URL,以便在 iOS 应用中快速退出 oauth2 会话。

网址是这样的: 注意有些参数是 URL 编码的

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

我在数据任务中使用此 URL 时遇到运行时错误:

Error Domain=NSURLErrorDomain Code=-1002 "unsupported URL" 
UserInfo={NSUnderlyingError=0x60000202ccf0 
{Error Domain=kCFErrorDomainCFNetwork Code=-1002 "(null)"}, 

NSErrorFailingURLStringKey=myapp://, NSErrorFailingURLKey=myapp://,

NSLocalizedDescription=unsupported URL}

我的 info.plist 中已经有“myapp”。

    <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>

下面是执行这个简单的 URLSession 数据任务的代码

        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()

我已尝试使用下面的两个 URL 字符串,但仍然出现相同的错误

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")!        

为什么 swift 不像 "myapp://" 或 (myapp%3A%2F%2F) 作为查询字符串参数?

更新 经过更多的实验,真正奇怪的是,如果我只有 1 个以方案作为值的查询字符串参数,则没有问题。似乎是一个错误,它不支持超过 1 个以方案作为值的查询字符串参数。

【问题讨论】:

标签: ios swift url urlsession


【解决方案1】:

这似乎是一个编码问题。您是否尝试过这样的编码:

let escapedString = originalString.addingPercentEncoding(withAllowedCharacters: .urlHostAllowed)

【讨论】:

猜你喜欢
  • 2015-02-14
  • 2013-12-23
  • 2010-09-29
  • 1970-01-01
  • 2019-10-04
  • 2012-07-30
  • 1970-01-01
  • 1970-01-01
  • 2014-07-09
相关资源
最近更新 更多