AFNetworking token

2016-08-15  本文已影响377人  227f600b867c

在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;
}
上一篇 下一篇

猜你喜欢

热点阅读