Welcome to the StravaSwift documentation! This documentation provides detailed information on how to use the StravaSwift library to interact with the Strava API.
Below, you can find comprehensive information on the various features and functionalities offered by StravaSwift.
/* Start of ‘Getting Started’ section */
# Getting Started
To get started with StravaSwift, you can follow the steps below:
1. Install StravaSwift through Cocoapods by adding the following line to your Podfile:
“`ruby
pod ‘StravaSwift’
“`
2. Run `pod install` in Terminal to install the library.
3. Import the StravaSwift module in your project:
“`swift
import StravaSwift
“`
4. Register your application with Strava to obtain the required credentials (client ID and client secret). You can register your app [here](https://developers.strava.com/).
5. Initialize the Strava client in your code:
“`swift
let stravaClient = StravaClient(clientId: “YOUR_CLIENT_ID”, clientSecret: “YOUR_CLIENT_SECRET”)
“`
6. Authenticate the user with Strava by using the following method:
“`swift
stravaClient.oauthUrl(scopes: [.activityRead, .profile], state: “YOUR_STATE_STRING”)
“`
This method will return a URL that you can present to the user for authentication.
7. After the user completes the authentication process, handle the callback URL using the following method:
“`swift
stravaClient.getAuthToken(from: url) { (result) in
switch result {
case .success(let token):
print(“Access token: \(token.accessToken)”)
case .failure(let error):
print(“Authentication failed with error: \(error)”)
}
}
“`
Upon successful authentication, you will receive an access token.
8. You can now use the StravaSwift library to interact with the Strava API using the authenticated `stravaClient` object.
/* Start of ‘Authentication’ section */
# Authentication
The StravaSwift library provides easy-to-use methods for authenticating users with Strava.
### oauthUrl(scopes: [StravaScope], state: String) -> URL
This method generates the URL that should be presented to the user for authentication.
**Parameters:**
– `scopes`: An array of `StravaScope` enums specifying the desired permissions.
– `state`: A unique string value that will be returned by Strava as an additional security measure.
**Example usage:**
“`swift
stravaClient.oauthUrl(scopes: [.activityRead, .profile], state: “YOUR_STATE_STRING”)
“`
### getAuthToken(from url: URL, completion: @escaping (Result
This method handles the callback URL received after successful authentication. It retrieves the access token from the callback URL.
**Parameters:**
– `url`: The URL received as a callback after user authentication.
– `completion`: A closure that is called upon retrieval of the access token. The closure has a `Result` object with either the access token or an error.
**Example usage:**
“`swift
stravaClient.getAuthToken(from: url) { (result) in
switch result {
case .success(let token):
print(“Access token: \(token.accessToken)”)
case .failure(let error):
print(“Authentication failed with error: \(error)”)
}
}
“`
/* Start of ‘Activities’ section */
# Activities
The StravaSwift library offers various methods to interact with a user’s activities on Strava.
### getAthleteActivities(completion: @escaping (Result<[StravaActivity], Error>) -> Void)
This method retrieves the logged-in athlete’s activities.
**Parameters:**
– `completion`: A closure that is called upon retrieval of the activities. The closure has a `Result` object with either the activities or an error.
**Example usage:**
“`swift
stravaClient.getAthleteActivities { (result) in
switch result {
case .success(let activities):
for activity in activities {
print(“Activity ID: \(activity.id)”)
}
case .failure(let error):
print(“Failed to retrieve activities with error: \(error)”)
}
}
“`
/* End of ‘Activities’ section */