Created
May 15, 2018 07:42
-
-
Save kfurue/b75afbd72e3070398f6f587963333c69 to your computer and use it in GitHub Desktop.
iOSアプリでOAuth認証 ref: https://qiita.com/kfurue/items/276cd17a99ad75c2be37
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
override func viewDidAppear(_ animated: Bool) { | |
let keychain = Keychain(service: "kfurue.RxSwiftRailsTutorial-token") | |
if keychain["oauthToken"] == nil { | |
oauthswift.authorizeURLHandler = SafariURLHandler(viewController: self, oauthSwift: oauthswift) | |
oauthswift.authorize( | |
withCallbackURL: URL(string: "sample-app://oauth-callback")!, | |
scope: "", state: "hoge", | |
success: { credential, _, _ in | |
print(credential.oauthToken) | |
// OAuthSwiftで取得したトークンをKeychainにストアする | |
keychain["oauthToken"] = credential.oauthToken | |
}, | |
failure: { error in | |
print(error.localizedDescription) | |
} | |
) | |
} | |
} |
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
let keychain = Keychain(service: "kfurue.RxSwiftRailsTutorial-token") | |
guard let token = (try? keychain.get("oauthToken")) as? String else { | |
return | |
} |
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
SampleAppClientAPI.customHeaders["Authorization"] | |
= "Bearer " + token | |
feedTableView.register(UITableViewCell.self, forCellReuseIdentifier: "Cell") | |
viewModel.feeds.asDriver().drive( | |
feedTableView.rx.items(cellIdentifier: "Cell"), | |
curriedArgument: {_, micropost, cell in | |
cell.textLabel?.text = micropost.content | |
}).disposed(by: disposeBag) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment