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 { | |
struct ClientCredentials { | |
static let APIKey = "YOUR_API_KEY" | |
static let APIKeySecret = "YOUR_API_KEY_SECRET" | |
static let CallbackURLScheme = "YOUR_CALLBACK_URL_SCHEME" | |
} | |
} |
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 | |
@main | |
struct TwitterTutorialApp: App { | |
@StateObject var twitterAPI = TwitterAPI() //1 | |
var body: some Scene { | |
WindowGroup { | |
ContentView() | |
.environmentObject(twitterAPI) //2 |
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 Combine // 1 | |
class TwitterAPI: NSObject, ObservableObject { | |
struct ClientCredentials { /*(...)*/ } | |
lazy var onOAuthRedirect = PassthroughSubject<URL, Never>() // 2 | |
} |
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 | |
@main | |
struct TwitterTutorialApp: App { | |
@StateObject var twitterAPI = TwitterAPI() | |
var body: some Scene { | |
WindowGroup { | |
ContentView() | |
.environmentObject(twitterAPI) |
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
extension CharacterSet { | |
static var urlRFC3986Allowed: CharacterSet { | |
CharacterSet(charactersIn: "-_.~").union(.alphanumerics) | |
} | |
} | |
extension String { | |
var oAuthURLEncodedString: String { | |
self.addingPercentEncoding(withAllowedCharacters: .urlRFC3986Allowed) ?? self | |
} |
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
extension String { | |
var urlQueryItems: [URLQueryItem]? { | |
URLComponents(string: "://?\(self)")?.queryItems | |
} | |
} | |
extension Array where Element == URLQueryItem { | |
func value(for name: String) -> String? { | |
return self.filter({$0.name == name}).first?.value | |
} |
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 CommonCrypto | |
extension String { | |
func hmacSHA1Hash(key: String) -> String { | |
var digest = [UInt8](repeating: 0, count: Int(CC_SHA1_DIGEST_LENGTH)) | |
CCHmac(CCHmacAlgorithm(kCCHmacAlgSHA1), | |
key, | |
key.count, | |
self, | |
self.count, |
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
private func oAuthSignatureBaseString(httpMethod: String, | |
baseURLString: String, | |
parameters: [URLQueryItem]) -> String { | |
var parameterComponents: [String] = [] | |
for parameter in parameters { | |
let name = parameter.name.oAuthURLEncodedString | |
let value = parameter.value?.oAuthURLEncodedString ?? "" | |
parameterComponents.append("\(name)=\(value)") | |
} | |
let parameterString = parameterComponents.sorted().joined(separator: "&") |
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
private func oAuthSigningKey(consumerSecret: String, | |
oAuthTokenSecret: String?) -> String { | |
if let oAuthTokenSecret = oAuthTokenSecret { | |
return consumerSecret.oAuthURLEncodedString + "&" + | |
oAuthTokenSecret.oAuthURLEncodedString | |
} else { | |
return consumerSecret.oAuthURLEncodedString + "&" | |
} | |
} |
OlderNewer