I have trouble using the new ASWebAuthenticationSession class that is used for the OAuth2 process in iOS applications.
I want to use this class to connect my app with my Strava account. I have the following code:
class AuthController
{
private var authSession: ASWebAuthenticationSession?
func authenticate()
{
let string = self.createWebAuthUrl()
guard let url = URL(string: string) else { return }
self.authSession = ASWebAuthenticationSession(url: url, callbackURLScheme: "MaccaStravaWebAuth://", completionHandler:
{
url, error in
})
self.authSession?.start()
}
fileprivate func createWebAuthUrl() -> String
{
var url = String.empty
url.append("https://www.strava.com/oauth/mobile/authorize")
url.append("?client_id=000000") // Removed for stackoverflow...
url.append("&redirect_uri=MaccaStravaWebAuth://test.com")
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
}
}
I added the custom URL scheme to my info.plist file. I changed the callback url in the sample code to "test.com", but in reality this is the exact same URL that is also used in my Strava application settings as the callback url.
When I call the authenticate() function, a dialog pops up and the Strava site is opened. When I click "Authorise", the completionHandler is not called (it is only called when I click Cancel).
Do I have to add something different? Are my callback urls valid? I tried several combinations of the callback URL scheme and my callback url that is set in Strava, but nothing worked.