Put羔砾、Delete、Get、Post封裝方法設(shè)置請(qǐng)求頭
還有獲取用戶權(quán)限以及登錄請(qǐng)求的封裝方法
先是不常用的吧(寫(xiě)的時(shí)候不是)
// GET超級(jí)api
+ (void)requestGETSuperAPIWithURLStr:(NSString *)urlStr WithAuthorization:(NSString *)authorization paramDic:(NSDictionary *)paramDic finish:(void(^)(id responseObject))finish enError:(void(^)(NSError *error))enError{
AFHTTPSessionManager *manager = [AFHTTPSessionManager manager];
manager.responseSerializer.acceptableContentTypes = [NSSet setWithObjects:@"text/html",@"application/json",@"text/javascript",@"text/json",@"text/plain",@"charset=UTF-8", nil];
// 設(shè)置請(qǐng)求頭
[manager.requestSerializer setValue:authorization forHTTPHeaderField:@"Authorization"];
[manager.requestSerializer setValue:@"application/json" forHTTPHeaderField:@"Accept"];
[manager GET:urlStr parameters:paramDic progress:nil success:^(NSURLSessionDataTask * _Nonnull task, id _Nullable responseObject) {
// 成功回調(diào)
finish(responseObject);
// 如果superapikey過(guò)期姜凄,重新保存加密獲取新的
// 如果用戶apikey過(guò)期政溃,則重新登錄
} failure:^(NSURLSessionDataTask * _Nullable task, NSError * _Nonnull error) {
// 失敗回調(diào)
enError(error);
}];
}
// POST請(qǐng)求 立即登錄
+ (void)requestLoginPOSTWithURLStr:(NSString *)urlStr WithAuthorization:(NSString *)authorization paramDic:(NSDictionary *)paramDic finish:(void(^)(id responseObject))finish enError:(void(^)(NSError *error))enError{
AFHTTPSessionManager *manager = [AFHTTPSessionManager manager];
manager.responseSerializer.acceptableContentTypes = [NSSet setWithObjects:@"text/html",@"application/json",@"text/javascript",@"text/json",@"text/plain",@"charset=UTF-8", nil];
// 設(shè)置請(qǐng)求頭
[manager.requestSerializer setValue:@"application/json" forHTTPHeaderField:@"Accept"];
[manager.requestSerializer setValue:authorization forHTTPHeaderField:@"Authorization"];
[manager POST:urlStr parameters:paramDic progress:nil success:^(NSURLSessionDataTask * _Nonnull task, id _Nullable responseObject) {
NSString *errcode = [NSString stringWithFormat:@"%@",[responseObject objectForKey:@"errcode"]];
if ([errcode isEqualToString:@"0"]) {
finish(responseObject);
}else {
NSString *errmsg = [responseObject objectForKey:@"errmsg"];
}
} failure:^(NSURLSessionDataTask * _Nullable task, NSError * _Nonnull error) {
// 失敗回調(diào)
enError(error);
}];
}
pragma mark - 請(qǐng)求數(shù)據(jù)GET和POST PUT
// GET 請(qǐng)求
+ (void)requestGETWithURLStr:(NSString *)urlStr paramDic:(NSDictionary *)paramDic Api_key:(NSString *)api_key finish:(void(^)(id responseObject))finish enError:(void(^)(NSError *error))enError{
AFHTTPSessionManager *manager = [AFHTTPSessionManager manager];
manager.responseSerializer.acceptableContentTypes = [NSSet setWithObjects:@"text/html",@"application/json",@"text/javascript",@"text/json",@"text/plain",@"charset=UTF-8", nil];
// 設(shè)置請(qǐng)求頭
[manager.requestSerializer setValue:@"application/json" forHTTPHeaderField:@"Accept"];
[manager.requestSerializer setValue:api_key forHTTPHeaderField:@"api_key"];
[manager GET:urlStr parameters:paramDic progress:nil success:^(NSURLSessionDataTask * _Nonnull task, id _Nullable responseObject) {
NSString *errcode = [NSString stringWithFormat:@"%@",[responseObject objectForKey:@"errcode"]];
if ([errcode isEqualToString:@"0"]) {
finish(responseObject);
}else {
NSString *errmsg = [responseObject objectForKey:@"errmsg"];
}
} failure:^(NSURLSessionDataTask * _Nullable task, NSError * _Nonnull error) {
enError(error);
}];
}
// POST請(qǐng)求
+ (void)requestPOSTWithURLStr:(NSString *)urlStr paramDic:(NSDictionary *)paramDic Api_key:(NSString *)api_key finish:(void(^)(id responseObject))finish enError:(void(^)(NSError *error))enError{
AFHTTPSessionManager *manager = [AFHTTPSessionManager manager];
manager.responseSerializer.acceptableContentTypes = [NSSet setWithObjects:@"text/html",@"application/json",@"text/javascript",@"text/json",@"text/plain",@"charset=UTF-8", nil];
// 設(shè)置請(qǐng)求頭
[manager.requestSerializer setValue:@"application/json" forHTTPHeaderField:@"Accept"];
[manager.requestSerializer setValue:api_key forHTTPHeaderField:@"api_key"];
[manager POST:urlStr parameters:paramDic progress:nil success:^(NSURLSessionDataTask * _Nonnull task, id _Nullable responseObject) {
NSString *errcode = [NSString stringWithFormat:@"%@",[responseObject objectForKey:@"errcode"]];
if ([errcode isEqualToString:@"0"]) {
finish(responseObject);
}else if ([errcode isEqualToString:@"100000"]) {
NSString *errmsg = [responseObject objectForKey:@"errmsg"];
// 彈出提示信息
[[NetworkManager shareNetworkManager].mdelegate showMessage:errmsg];
}else{
NSString *errmsg = [responseObject objectForKey:@"errmsg"];
}
} failure:^(NSURLSessionDataTask * _Nullable task, NSError * _Nonnull error) {
enError(error);
}];
}
pragma mark - PUT 請(qǐng)求
+ (void)requestPUTWithURLStr:(NSString *)urlStr paramDic:(NSDictionary *)paramDic Api_key:(NSString *)api_key finish:(void(^)(id responseObject))finish enError:(void(^)(NSError *error))enError{
AFHTTPSessionManager *manager = [AFHTTPSessionManager manager];
manager.responseSerializer.acceptableContentTypes = [NSSet setWithObjects:@"text/html",@"application/json",@"text/javascript",@"text/json",@"text/plain", nil];
// 設(shè)置請(qǐng)求頭
[manager.requestSerializer setValue:@"application/json" forHTTPHeaderField:@"Accept"];
[manager.requestSerializer setValue:api_key forHTTPHeaderField:@"api_key"];
[manager PUT:urlStr parameters:paramDic success:^(NSURLSessionDataTask * _Nonnull task, id _Nullable responseObject) {
NSString *errcode = [NSString stringWithFormat:@"%@",[responseObject objectForKey:@"errcode"]];
if ([errcode isEqualToString:@"0"]) {
finish(responseObject);
}else{
NSString *errmsg = [responseObject objectForKey:@"errmsg"];
}
} failure:^(NSURLSessionDataTask * _Nullable task, NSError * _Nonnull error) {
enError(error);
}];
}
//delete和put差不多态秧,沒(méi)有參數(shù)哦董虱,記得里面的方法要改
// 還有福利!
// 上傳圖片
+ (void)requestAddImgPOSTWithURLStr:(NSString *)url paramDic:(NSDictionary *)paramDic image:(UIImage *)image name:(NSString *)name success:(void (^)(id responseObject))success failure:(void (^)(NSError *error))failure{
// 1.創(chuàng)建網(wǎng)絡(luò)管理者
AFHTTPSessionManager *manager = [AFHTTPSessionManager manager];
manager.responseSerializer.acceptableContentTypes = [NSSet setWithObjects:@"text/html",@"application/json",@"text/javascript",@"text/json",@"text/plain", nil];
//請(qǐng)求圖片,請(qǐng)求網(wǎng)頁(yè)時(shí)需要加入這句,因?yàn)锳FN默認(rèn)的請(qǐng)求的是json
manager.responseSerializer = [AFHTTPResponseSerializer serializer];
manager.requestSerializer = [AFHTTPRequestSerializer serializer];
// 2.發(fā)送請(qǐng)求(字典只能放非文件參數(shù))
[manager POST:url parameters:paramDic constructingBodyWithBlock:^(id<AFMultipartFormData> _Nonnull formData) {
NSData *imageData = UIImageJPEGRepresentation(image, 0.5);//進(jìn)行圖片壓縮
// 使用日期生成圖片名稱
NSDateFormatter *formatter = [[NSDateFormatter alloc] init];
formatter.dateFormat = @"yyyyMMddHHmmss";
NSString *fileName = [NSString stringWithFormat:@"%@.png",[formatter stringFromDate:[NSDate date]]];
// 上傳圖片申鱼,以文件流的格式
// 任意的二進(jìn)制數(shù)據(jù)MIMEType application/octet-stream
// 特別注意愤诱,這里的圖片的名字不要寫(xiě)錯(cuò),必須是接口的圖片的參數(shù)名字如我這里是file
if (imageData!=nil) { // 圖片數(shù)據(jù)不為空才傳遞
[formData appendPartWithFileData:imageData name:name fileName:fileName mimeType:@"image/png"];
}
} progress:nil success:^(NSURLSessionDataTask * _Nonnull task, id _Nullable responseObject) {
NSDictionary *dic = [NSJSONSerialization JSONObjectWithData:responseObject options:(NSJSONReadingMutableContainers) error:nil];
if ([[dic objectForKey:@"status"]isEqualToString:@"success"]) {
success(responseObject);
}else{
NSString *message = [responseObject objectForKey:@"message"];
NSLog(@"messageImg == %@",message);
[[NetworkManager shareNetworkManager].mdelegate showMessage:message];
}
} failure:^(NSURLSessionDataTask * _Nullable task, NSError * _Nonnull error) {
failure(error);
}];
}