This is a Swift wrapper for the Strava v3 API.
As this is a passion project, I only work on it when I have time. So, if you are interested in contributing, feel free to submit PRs.
To run the example project, clone the repo, and run pod install from the Example directory first.
StravaSwift is available through the Swift Package Manager and CocoaPods.
dependencies: [
.package(url: "https://github.com/mpclarkson/StravaSwift.git", from: "1.0.1")
]To install it, simply add the following line to your Podfile:
pod "StravaSwift", '~> 1.0.1'The full library documentation is available here.
-
Register your app with Strava and get an OAuth
client idandclient secret. -
Register your redirect URL scheme in your
info.plistfile.The authentication will use the Strava app be default if it is installed on the device. If the user does not have Strava installed, it will fallback on
SFAuthenticationSessionorASWebAuthenticationSessiondepending on the iOS version. If your app is linked on or after iOS 9.0, you must addstravain you app’sinfo.plistfile. It should be added as an entry fo the array under theLSApplicationQueriesSchemeskey. Failure to do this will result in a crash when callingcanOpenUrl:.<key>LSApplicationQueriesSchemes</key> <array> <string>strava</string> </array>
-
Implement the following method in your
AppDelegate.swiftto handle the OAuth redirection from Strava:NOTE: by default the OAuth token is only available while the app is running, which means you need to request a new token. You can implement custom token storage and retrieval behaviour by overriding the default token
delegatein theStravaConfiginitializer which must implement theTokenDelegateprotocol.UIKit Initialize the Strava Client as follows, preferably in your
AppDelegate.swiftto ensure it is configured before you call it:let config = StravaConfig( clientId: YourStravaClientId, clientSecret: YourStravaClientSecret, redirectUri: YourRedirectUrl ) let strava = StravaClient.sharedInstance.initWithConfig(config) func application(_ app: UIApplication, open url: URL, options: [UIApplication.OpenURLOptionsKey : Any] = [:]) -> Bool { return strava.handleAuthorizationRedirect(url) }
SwiftUI
@main struct MyApplication: App { var strava = StravaClient.sharedInstance.initWithConfig(StravaConfig( clientId: YourStravaClientId, clientSecret: YourStravaClientSecret, redirectUri: YourRedirectUrl )) var body: some Scene { WindowGroup { ContentView() .onOpenURL { url in strava.handleAuthorizationRedirect(url) } } } }
-
After authorizing, you can start requesting resources:
strava.authorize() { result in switch result { case .success(let token): //do something for success case .failure(let error): //do something for error } }
The Router implementation is based on this Alamofire example:
strava.request(Router.athletes(id: 9999999999)) { (athlete: Athlete?) in
//do something with the athlete
}
let params = [
'page' = 2,
'per_page' = 25
]
strava.request(Router.athleteActivities(params: params) { (activities: [Activity]?) in
//do something with the activities
}- 100% API coverage (about 90% now)
- Documentation
- Tests
- Better example app
Matthew Clarkson, [email protected]
StravaSwift is available under the MIT license. See the LICENSE file for more info.