在http请求中身份验证,加token。一般加在http header里,在AFNetworking中,一般请求:
@interface XLNetworkClient () {
NSString *_token;
}
@property (nonatomic, strong) AFHTTPSessionManager *manager;
@end
@implementation XLNetworkClient
+ (instancetype)sharedClient {
static XLNetworkClient *sharedClient;
static dispatch_once_t onceToken;
dispatch_once(&onceToken, ^{
sharedClient = [[XLNetworkClient alloc] init];
sharedClient.manager = [AFHTTPSessionManager manager];
// 将token加到http头里
[sharedClient.manager.requestSerializer setValue:sharedClient.token forHTTPHeaderField:@"authorization"];
});
return sharedClient;
}
网友评论