1

ASWebAuthenticationSession completionHandler not called on clicking Authorize button in Strava Authentication. below given is my code.(used Using ASWebAuthenticationSession to connect to Strava account fails this link to implement.)

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

and my info.plist file looks like enter image description here

Ben Rockey
  • 920
  • 6
  • 23

2 Answers2

3

I got it by changing my code like..

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

enter image description here

Change <client id> with your client id and abcd with your app name..

Ben Rockey
  • 920
  • 6
  • 23
0

I think you are correct in changing the URL Scheme format to abcd. However, the value of the callbackURLScheme parameter passed when initializing ASWebAuthenticationSession must be the same as this URL Scheme(abcd). Therefore, your code should be changed as follows:

self.authSession = ASWebAuthenticationSession(url: url, callbackURLScheme: "abcd", completionHandler:
{ url, error in

 })

You may also refer to Apple's official documentation about how to use ASWebAuthenticationSession.

Kazunori Takaishi
  • 2,268
  • 1
  • 15
  • 27