把數據請求和風火輪的效果封裝成一個方法,用的時候直接調用這個方法就可以 + (void)startRequest:(NSString*)method baseurl:(NSString*)baseurl param:(NSDictionary*)params success:(DKSuccess)s ...
把數據請求和風火輪的效果封裝成一個方法,用的時候直接調用這個方法就可以
+ (void)startRequest:(NSString*)method
baseurl:(NSString*)baseurl
param:(NSDictionary*)params
success:(DKSuccess)success
failure:(DKFailure)failure
showProgress:(BOOL)progress{
NSAssert(params, @"params is nil.");
NSDictionary *par = @{@"reqData":[self dictionaryToString:params]};
if (progress) {
[SVProgressHUD showWithMaskType:SVProgressHUDMaskTypeClear];
}
AFHTTPSessionManager *manager = [AFHTTPSessionManager manager];
manager.requestSerializer = [AFHTTPRequestSerializer serializer];
manager.responseSerializer = [AFHTTPResponseSerializer serializer];
if ([method isEqualToString:Get]) {
[manager GET:baseurl parameters:par progress:^(NSProgress * _Nonnull downloadProgress) {
} success:^(NSURLSessionDataTask * _Nonnull task, id _Nonnull responseObject) {
[SVProgressHUD dismiss];
NSAssert(responseObject, @"responseObject is nil.");
success(task, [self decryptData:responseObject]);
} failure:^(NSURLSessionDataTask * _Nullable task, NSError * _Nonnull error) {
[SVProgressHUD dismiss];
failure(task, error);
}];
}else{
[manager POST:baseurl parameters:par progress:^(NSProgress * _Nonnull uploadProgress) {
} success:^(NSURLSessionDataTask * _Nonnull task, id _Nonnull responseObject) {
[SVProgressHUD dismiss];
NSAssert(responseObject, @"responseObject is nil.");
success(task, [self decryptData:responseObject]);
} failure:^(NSURLSessionDataTask * _Nullable task, NSError * _Nonnull error) {
[SVProgressHUD dismiss];
failure(task, error);
}];
}
}