不同iPhone机型下Spotify Web API认证失败问题排查
问题背景
我开发了一款集成Spotify Web API的iOS应用,在iPhone 14 Pro模拟器上测试完全正常,但部署到iPhone 13 Pro Max真机,或者在iPhone 14 Pro Max模拟器运行时都会崩溃。核心问题是不同设备上重定向URL返回的query items不一致。
相关代码
提取query items的WKWebView代理函数如下:
func webView(_ webView: WKWebView, decidePolicyFor navigationAction: WKNavigationAction, decisionHandler: @escaping (WKNavigationActionPolicy) -> Void) { guard let url = navigationAction.request.url else { decisionHandler(.cancel) return } //print(self.codeVerifier) if url.absoluteString.starts(with: redirectURI) { if let urlComponents = URLComponents(url: url, resolvingAgainstBaseURL: true), let queryItems = urlComponents.queryItems { // Extract the authorization code from the query items var code: String? for item in queryItems { if item.name == "code" { code = item.value break } } print(queryItems) // Verify the code verifier if let code = code, let storedCodeVerifier = self.codeVerifier, verifyCodeVerifier(storedCodeVerifier) { requestAccessToken(code: code) signOutButton.isHidden = false displayDataButton.isHidden = false } print("hi") webView.isHidden = true } } decisionHandler(.allow) }
现象对比
- 在iPhone 14 Pro模拟器中,打印的query items符合预期:
[code=AQDYBJnCNU7O17gFSog0aUvM3LjjigPQAMQLDE8q7ucdAZo7arEw1rxdJcysAB7gxhuT_2szj8z6XvnJnsaGomUt0YgvpkGjTnq_Gjr6TMScWmPxlHgPg4Bwy-iflCFq8_gYZlTXjy-WokNjRq0FYmvgVbDcM14c8fqMQ8G32IZnDxfO8GQbcN1bUuSNOVTgrTWvmQOKThHVWiIpOSbAf3hc_CuZvKzBurOi-Rv5KTgGSVKbnWa_tv42JtVpyckDOGkT-z0Wnhyj1Ig8c8jhVEvbcK43fkdDQQMD59aPZ0PPj_0rkBFtGowVxvE]
- 在iPhone 14 Pro Max模拟器中,打印的query items完全不符合预期:
[ar=1, k=6LfCVLAUAAAAALFwwRnnCJ12DalriUGbj8FW_J39,
co=aHR0cHM6Ly9hY2NvdW50cy5zcG90aWZ5LmNvbTo0NDM., hl=en,
v=khH7Ei3klcvfRI74FvDcfuOo, size=invisible, cb=dxrrvlwvv8sx]
疑问
请问可能是什么原因导致的?
内容的提问来源于stack exchange,提问作者AwesomeCuber

