在Strava身份验证中单击授权按钮时,未调用ASWebAuthenticationSession completionHandler。下面给出了我的代码(使用Using ASWebAuthenticationSession to connect to Strava account fails这个链接来实现)。
import AuthenticationServices
class LinkAppsViewController: UIViewController, ASWebAuthenticationPresentationContextProviding{
func authenticate()
{
let string = self.createWebAuthUrl()
guard let url = URL(string: string) else { return }
self.authSession = ASWebAuthenticationSession(url: url, callbackURLScheme: "localhost/", completionHandler:
{
url, error in
})
self.authSession!.presentationContextProvider = self
self.authSession?.start()
}
fileprivate func createWebAuthUrl() -> String
{
var url = String.init()
url.append("https://www.strava.com/oauth/mobile/authorize")
url.append("?client_id=<client id>")
url.append("&redirect_uri=http://localhost/exchange_token")
url.append("&response_type=code")
url.append("&approval_prompt=force")
url.append("&grant_type=authorization_code")
url.append("&scope=activity:write,activity:read_all")
return url
}
func presentationAnchor(for session: ASWebAuthenticationSession) -> ASPresentationAnchor {
print(session)
return view.window!
}
}我的info.plist文件看起来像这样

发布于 2020-08-28 14:26:18
我是通过修改我的代码得到它的..
fileprivate func createWebAuthUrlStrava() -> String
{
var url = String.init()
url.append("https://www.strava.com/oauth/mobile/authorize")
url.append("?client_id=<client id>")
url.append("&redirect_uri=abcd://localhost/exchange_token")
url.append("&response_type=code")
url.append("&approval_prompt=force")
url.append("&grant_type=authorization_code")
url.append("&scope=activity:write,activity:read_all")
return url
}info.plist

将<client id>更改为您的客户端id,并将and更改为您的应用程序名称。
发布于 2021-06-05 03:36:12
我认为您将URL Scheme格式更改为abcd是正确的。但是,在初始化ASWebAuthenticationSession时传递的callbackURLScheme参数的值必须与此URL方案(abcd)相同。因此,您的代码应按如下方式更改:
self.authSession = ASWebAuthenticationSession(url: url, callbackURLScheme: "abcd", completionHandler:
{ url, error in
})您还可以参考Apple's official documentation了解如何使用ASWebAuthenticationSession。
https://stackoverflow.com/questions/60000470
复制相似问题