https://github.com/fromkk/oauth2client_ios
https://github.com/fromkk/oauth2client_ios
Last synced: about 1 year ago
JSON representation
- Host: GitHub
- URL: https://github.com/fromkk/oauth2client_ios
- Owner: fromkk
- Created: 2015-11-17T10:07:49.000Z (over 10 years ago)
- Default Branch: master
- Last Pushed: 2015-11-17T10:20:50.000Z (over 10 years ago)
- Last Synced: 2025-02-03T14:27:38.387Z (over 1 year ago)
- Language: Objective-C
- Size: 89.8 KB
- Stars: 0
- Watchers: 2
- Forks: 0
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
Awesome Lists containing this project
README
# iOS でOAuthライブラリを利用する方法
今回選定したのは [NXOAuth2Client](https://github.com/nxtbgthng/OAuth2Client)
※基本的にライブラリのREADME.mdに書いてます
## 初期設定
```objective-c
//AppDelegate.mとかでいいと思う
[[NXOAuth2AccountStore sharedStore] setClientID:@"xXxXxXxXxXxX"
secret:@"xXxXxXxXxXxX"
authorizationURL:[NSURL URLWithString:@"https://...your auth URL..."]
tokenURL:[NSURL URLWithString:@"https://...your token URL..."]
redirectURL:[NSURL URLWithString:@"https://...your redirect URL..."]
forAccountType:@"famm"];
```
## ログイン
```objective-c
[[NXOAuth2AccountStore sharedStore] requestAccessToAccountWithType:@"famm"
username:aUserName
password:aPassword];
```
## ログイン成功時
`NSNotificationCenter` に `NXOAuth2AccountStoreAccountsDidChangeNotification` のキーで通知を受け取る
ログインしたアカウントは `NSNotification` から `userInfo` の `NXOAuth2AccountStoreNewAccountUserInfoKey` キーで取得出来る
※[参考](http://hack.aipo.com/archives/8853/)
## ログイン失敗時
`NSNotificationCenter` に `addObserverForName:NXOAuth2AccountStoreDidFailToRequestAccessNotification` のキーで通知を受け取る
## APIにリクエストする方法
```objective-c
[NXOAuth2Request performMethod:@"GET"
onResource:[NSURL URLWithString:@"https://...your service URL..."]
usingParameters:nil
withAccount:loginedAccount
sendProgressHandler:^(unsigned long long bytesSend, unsigned long long bytesTotal) { // e.g., update a progress indicator }
responseHandler:^(NSURLResponse *response, NSData *responseData, NSError *error){
// Process the response
}];
```
## 承認済みのURLを取得
```objective-c
NXOAuth2Request *theRequest = [[NXOAuth2Request alloc] initWithResource:[NSURL URLWithString:@"https://...your service URL..."]
method:@"GET"
parameters:nil];
theRequest.account = // ... an loginedAccount
NSURLRequest *sigendRequest = [theRequest signedURLRequest];
```