Last active
August 29, 2015 13:57
-
-
Save gologo13/9590868 to your computer and use it in GitHub Desktop.
ヘッダのカスタマイズや独自処理をしたいときの AFNetworking 2.0 の使い方 ref: http://qiita.com/gologo13/items/f36cb7208c15869888ca
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
AFHTTPRequestOperationManager *manager = [AFHTTPRequestOperationManager manager]; | |
[manager GET:@"https://hogehoge.api.com/user" | |
parameters:@{ @"user_id":@"hoge" } | |
success:^(AFHTTPRequestOperation *operation, id responseObject) { | |
NSLog(@"res:%@", responseObject); | |
} | |
failure:^(AFHTTPRequestOperation *operation, NSError *error) { | |
NSLog(@"error:%@", error); | |
}]; | |
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
AFHTTPRequestOperationManager *manager = [AFHTTPRequestOperationManager manager]; | |
[manager.requestSerializer setValue:@"SomeValue" forHTTPHeaderField:@"SomeHeaderField"] |
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
- (void)setAuthorizationHeaderFieldWithUsername:(NSString *)username | |
password:(NSString *)password; | |
... | |
[manager.requestSerializer setAuthorizationHeaderFieldWithUsername:@"username" | |
password:@"xxx"]; | |
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
AFHTTPRequestOperationManager *manager = [AFHTTPRequestOperationManager manager]; | |
AFHTTPRequestSerializer *selializer = [AFHTTPRequestSerializer serializer]; | |
self.requestSerializer = selializer; | |
NSString *URLString = @"https://api.twitter.com/1.1/friendships/create.json"; | |
// リクエストに使う NSURLRequest を生成 | |
// このメソッドを使うことで、AFNetworkingのパラメータのエンコードを良しなにやってくれる処理を再利用できる | |
NSMutableURLRequest *request = | |
[selializer requestWithMethod:@"POST" | |
URLString:URLString | |
parameters:@{ @"screen_name":@"gologo13" } | |
error:nil]; | |
// 独自の処理 リクエストヘッダに Twitter API を叩くためのアクセストークンを付与する | |
[[PFTwitterUtils twitter] signRequest:request]; | |
// リクエスト開始 | |
AFHTTPRequestOperation *operation = [self HTTPRequestOperationWithRequest:request success:success failure:failure]; | |
[self.operationQueue addOperation:operation]; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment