封装一个AFN处理请求头 和 请求内容
/** * 对AFNetWorking进行基本封装(不处理code值, 不显示HUD) * * @param urlString 请求url * @param parameters 参数 * @param success 请求成功,含code值的 数据 * @param failure 请求失败 */
+ (void)baseNetworkingWithUrlString:(NSString *)urlString parameters:(id)parameters success:(NetWorkSuccess) success failure:(NetWorkFailure)failure {
// 判断网络
if ([[YJNetWorkMonitor shareInstance] rechability] == false) {
// 提示网络
[MBProgressHUD showSimpleTextOnlyHUD:kNetWorkingCloseText withDelayTime:MIDDLE_TIME];
return;
}
NSURLSessionConfiguration *sessionConfiguration = [NSURLSessionConfiguration defaultSessionConfiguration];
AFHTTPSessionManager *manager = [[AFHTTPSessionManager alloc] initWithSessionConfiguration:sessionConfiguration];
[manager.securityPolicy setAllowInvalidCertificates:NO];
manager.requestSerializer = [AFHTTPRequestSerializer serializer]; // 字典
manager.responseSerializer.acceptableContentTypes = [NSSet setWithObjects:@"application/json", @"text/json", @"text/javascript",@"text/html", nil];
[manager.requestSerializer setValue:[UserInfoUserDefault getUserToken] forHTTPHeaderField:@"token"]; // 设置请求头
// 请求Url 判断拼接
NSString *tempUrlString = [NSString stringWithFormat:@"%@%@?", URL_HEADER_SETVERURL, urlString];
if (parameters == nil) {
tempUrlString = [NSString stringWithFormat:@"%@%@", URL_HEADER_SETVERURL, urlString];
}
// 发送请求
[manager POST:tempUrlString parameters:parameters progress:^(NSProgress * _Nonnull uploadProgress) {
// 进度
} success:^(NSURLSessionDataTask * _Nonnull task, id _Nullable responseObject) {
// 成功回调
success(responseObject);
} failure:^(NSURLSessionDataTask * _Nullable task, NSError * _Nonnull error) {
[MBProgressHUD showSimpleTextOnlyHUD:kNetWorkingErrorText withDelayTime:MIDDLE_TIME];
// 失败回调
failure(error);
LBLog(@"error === %@", error);
}];
}
本站转载的文章为个人学习借鉴使用,本站对版权不负任何法律责任。如果侵犯了您的隐私权益,请联系我们删除。