This file contains 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
struct TokenCredentials { | |
let accessToken: String | |
let accessTokenSecret: String | |
} |
This file contains 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
func oAuthAccessTokenPublisher(temporaryCredentials: TemporaryCredentials, verifier: String) -> AnyPublisher<(TokenCredentials, User), OAuthError> { | |
// 1 | |
let request = (baseURLString: "https://api.twitter.com/oauth/access_token", | |
httpMethod: "POST", | |
consumerKey: ClientCredentials.APIKey, | |
consumerSecret: ClientCredentials.APIKeySecret) | |
// 2 | |
guard let baseURL = URL(string: request.baseURLString) else { | |
return Fail(error: OAuthError.urlError(URLError(.badURL))) |
This file contains 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 SwiftUI | |
struct ContentView: View { | |
@EnvironmentObject var twitterAPI: TwitterAPI // 1 | |
var body: some View { | |
VStack { | |
if let screenName = twitterAPI.user?.screenName { // 2 | |
Text("Welcome").font(.largeTitle) | |
Text(screenName).font(.largeTitle) |
This file contains 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 TwitterAPI: NSObject, ObservableObject { | |
@Published var authorizationSheetIsPresented = false // 1 | |
@Published var authorizationURL: URL? // 2 | |
@Published var user: User? // 3 | |
struct User { | |
let ID: String | |
let screenName: String | |
} | |
This file contains 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 SwiftUI | |
import SafariServices | |
struct SafariView: UIViewControllerRepresentable { | |
// 1 | |
class SafariViewControllerWrapper: UIViewController { | |
// 2 | |
private var safariViewController: SFSafariViewController? | |
// 3 |
This file contains 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
func oAuthRequestTokenPublisher() -> AnyPublisher<TemporaryCredentials, OAuthError> { | |
// 1 | |
let request = (baseURLString: "https://api.twitter.com/oauth/request_token", | |
httpMethod: "POST", | |
consumerKey: ClientCredentials.APIKey, | |
consumerSecret: ClientCredentials.APIKeySecret, | |
callbackURLString: "\(ClientCredentials.CallbackURLScheme)://") | |
// 2 | |
guard let baseURL = URL(string: request.baseURLString) else { |
This file contains 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
struct TemporaryCredentials { | |
let requestToken: String | |
let requestTokenSecret: String | |
} |
This file contains 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
enum OAuthError: Error { | |
case unknown | |
case urlError(URLError) | |
case httpURLResponse(Int) | |
case cannotDecodeRawData | |
case cannotParseResponse | |
case unexpectedResponse | |
case failedToConfirmCallback | |
} |