This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| import UIKit | |
| import AuthenticationServices | |
| class ViewController: UIViewController { | |
| private var authSession: ASWebAuthenticationSession? | |
| private let clientId: String = "11111" | |
| private let urlScheme: String = "youUrlScheme" | |
| private let fallbackUrl: String = "yourFallbackUrl" |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| private let clientSecret: String = "yourClientSecret" | |
| private func requestStravaTokens(with code: String) { | |
| let parameters: [String: Any] = ["client_id": clientId, "client_secret": clientSecret, "code": code, "grant_type": "authorization_code"] | |
| AF.request("https://www.strava.com/oauth/token", method: .post, parameters: parameters).response { response in | |
| guard let data = response.data else { return } | |
| if let json = try? JSONSerialization.jsonObject(with: data, options: []) { | |
| print(json) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| private func presentStravaAuthentication() { | |
| let url: String = "https://www.strava.com/oauth/mobile/authorize?client_id=\(clientId)&redirect_uri=\(urlScheme)%3A%2F%2F\(fallbackUrl)&response_type=code&approval_prompt=auto&scope=read" | |
| guard let authenticationUrl = URL(string: url) else { return } | |
| authSession = ASWebAuthenticationSession(url: authenticationUrl, callbackURLScheme: "\(urlScheme)://") { [weak self] url, error in | |
| if let error = error { | |
| print(error) | |
| } else { | |
| if let code = self?.getCode(from: url) { | |
| print(code) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| private func getCode(from url: URL?) -> String? { | |
| guard let url = url?.absoluteString else { return nil } | |
| let urlComponents: URLComponents? = URLComponents(string: url) | |
| let code: String? = urlComponents?.queryItems?.filter { $0.name == "code" }.first?.value | |
| return code | |
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| extension ViewController: ASWebAuthenticationPresentationContextProviding { | |
| func presentationAnchor(for session: ASWebAuthenticationSession) -> ASPresentationAnchor { | |
| UIApplication.shared.windows[0] | |
| } | |
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| import UIKit | |
| import AuthenticationServices | |
| class ViewController: UIViewController { | |
| private var authSession: ASWebAuthenticationSession? | |
| private let clientId: String = "11111" | |
| private let urlScheme: String = "youUrlScheme" | |
| private let fallbackUrl: String = "yourFallbackUrl.com" |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| class ViewController: UIViewController { | |
| @IBAction private func connectToStrava(_ sender: UIButton) { | |
| presentStravaAuthentication() | |
| } | |
| private func presentStravaAuthentication() { | |
| } | |