I have used this library. https://github.com/mpclarkson/StravaSwift

My callback url redirects me to any browser link but does not redirect me to my app.How can I redirect back to my app after authentication from strava?

2

There are 2 best solutions below

0
On

To have strava redirect back to your app, you need to register a url scheme in your info.plist file and then use that url scheme to allow the OS to recognize the redirect.

Here are some steps that should help:

  1. Register url scheme in your info.plist. To do this, add a unique entry under LSApplicationQueriesSchemes in info.plist, "myApp" for example. If you do not already have LSApplicationQueriesSchemes in info.plist as an entry, you will need to create this (needs to be an array).

  2. Register authorization callback domain with Strava at https://www.strava.com/settings/api, along the lines of myApp.com.

  3. Assign the callbackURLScheme to match the registered url scheme in your info.plist (from step 1, "myApp"). To illustrate:

private var authSession: ASWebAuthenticationSession?

let appOAuthUrlStravaScheme = URL(string: "strava://oauth/mobile/authorize?client_id=1234321&redirect_uri=myApp%3A%2F%2FmyApp.com&response_type=code&approval_prompt=auto&scope=activity%3Awrite%2Cread&state=test")!

let webOAuthUrl = URL(string: "https://www.strava.com/oauth/mobile/authorize?client_id=1234321&redirect_uri= myApp%3A%2F%2FmyApp.com&response_type=code&approval_prompt=auto&scope=activity%3Awrite%2Cread&state=test")!


func authenticate() {
    if UIApplication.shared.canOpenURL(appOAuthUrlstravaScheme) {
        UIApplication.shared.open(appOAuthUrlstravaScheme, options: [:])
    } else {
        authSession = ASWebAuthenticationSession(url: webOAuthUrl, callbackURLScheme: "myApp") { url, error in
            // Code after redirect
        }

        authSession?.start()
    }
}
1
On

OP Mentioned usage of this GitHub cocoa pods -> https://github.com/mpclarkson/StravaSwift

using the example app within the GitHub project, the following is what is needed for redirect back to the app after Strava oAuth authentication.

info.plist

<key>CFBundleURLTypes</key>
    <array>
        <dict>
            <key>CFBundleTypeRole</key>
            <string>Editor</string>
            <key>CFBundleURLName</key>
            <string>com.stravaswift.ios</string>
            <key>CFBundleURLSchemes</key>
            <array>
                <string>breakaway</string> <--- This is my App Name
            </array>
        </dict>
    </array>

Note that per the readme.md file for that project page you also need this

<key>LSApplicationQueriesSchemes</key>
<array>
    <string>strava</string>
</array>

Then within appDelegate.swift (also per the readme.md)

  let config = StravaConfig(
        clientId: <YOUR_CLIENT_ID>,
        clientSecret: <YOUR_CLIENT_SECRET>,
        redirectUri: "breakaway://breakaway701849232.wordpress.com",
    scopes: [.activityReadAll,.activityWrite]
    )

Edit for IOS14.5+ / Xcode 12.5+ Due to changes in ASWebuthentitor https://developer.apple.com/forums/thread/679251) I am getting errors with the above. The redirectUri is now changed to the below

  let config = StravaConfig(
        clientId: <YOUR_CLIENT_ID>,
        clientSecret: <YOUR_CLIENT_SECRET>,
  redirectUri: "breakaway://breakaway701849232.wordpress.com".addingPercentEncoding(withAllowedCharacters: .urlHostAllowed)!,
    scopes: [.activityReadAll,.activityWrite]
    )

Note that the redirectUri is "breakaway" :// "breakaway701849232.wordpress.com" where : breakaway - my redirectUri breakaway701849232.wordpress.com - my domain (That is inputed into Strava API for my app)

enter image description here

NOTE: One Gotcha I fell into was that the Scopes is actually a comma delimited array. I kept getting errors using the example app where it lists all the activities but it was because I did not include a "ReadAll" into the scope (My goal is to upload TCX files to Strava).