【发布时间】: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 个以方案作为值的查询字符串参数。
【问题讨论】:
-
您是否尝试删除百分比编码? developer.apple.com/documentation/foundation/nsstring/…你能把你的 URLSession 代码贴在这里吗?
-
更新了 URLSession 代码。
标签: ios swift url urlsession