#import <Foundation/Foundation.h> #import <UIKit/UIKit.h> @interface MyURLPost : NSObject //POST 带参数 +(void)post:(NSString *)url paramters:(NSDictionary *)param success:(void (^)(id responseObjt))success failure:(void (^)(NSError *error))failure; //发送GET请求 + (void)get:(NSString *)url params: (NSDictionary *)params success:(void(^)(id responseObjt))success failure:(void(^)(NSError *error))failure; //发送POST请求 + (void)post:(NSString *)url params:(NSDictionary *)params success:(void(^)(id responseObjt)) success failure:(void(^)(NSError *error))failure; //添加进度条 +(void)post:(NSString *)url params:(NSDictionary *)params success:(void(^)(id responseObjt)) success failure:(void(^)(NSError *error))failure withView:(UIView *)view; @end
#import "MyURLPost.h" #import "AFNetworking.h" #import "MYAlert.h" #import "NSObject+GetIP.h" #import "NSString+Hashing.h" @implementation MyURLPost //POST 带参数 + (void)post:(NSString *)url paramters:(NSDictionary *)param success:(void (^)(id))success failure:(void (^)(NSError *errror))failure{ //3.判断网络状况 AFNetworkReachabilityManager *netManager = [AFNetworkReachabilityManager sharedManager]; //开始监听 [netManager startMonitoring]; [netManager setReachabilityStatusChangeBlock:^(AFNetworkReachabilityStatus status){ if (status == AFNetworkReachabilityStatusNotReachable) { //showAlert [MYAlert alertWithTitle:@"提示" messages:@"网络链接错误,请检查网络链接" cancelTitle:@"我知道了" otherTitle:nil]; DLog(@"没有网络"); return; }else if (status == AFNetworkReachabilityStatusUnknown){ DLog(@"未知网络"); }else if (status == AFNetworkReachabilityStatusReachableViaWWAN){ DLog(@"WiFi"); }else if (status == AFNetworkReachabilityStatusReachableViaWiFi){ DLog(@"手机网络"); } }]; NSTimeInterval time = [[NSDate date]timeIntervalSince1970]; long i = time; NSString *paramTime = [NSString stringWithFormat:@"%ld",i]; NSString *ipstr = [NSObject deviceIPAdress]; NSString *paramIp = ipstr; NSString *signstr = [NSString stringWithFormat:@"%@%@%@%@",paramTime,paramIp,@"phone_ios",@"d556bd3337cd909b49eb5e33f46ad65c"]; NSString *md5str = [signstr MD5Hash]; NSString *mdstr = [md5str lowercaseString]; NSString *sign = mdstr; NSString *key = @"phone_ios"; NSDictionary *dict = @{ @"paramTime" : paramTime, @"paramIp" : paramIp, @"sign":sign, @"key" : key, @"param":param }; AFHTTPRequestOperationManager *manager = [AFHTTPRequestOperationManager manager]; manager.requestSerializer = [AFJSONRequestSerializer serializer]; manager.responseSerializer = [AFJSONResponseSerializer serializer]; manager.responseSerializer.acceptableContentTypes = [NSSet setWithObject:@"application/json"]; [manager POST:url parameters:dict success:^(AFHTTPRequestOperation *operation, id responseObject) { if (success) { success(responseObject); } } failure:^(AFHTTPRequestOperation *operation, NSError *error) { if (failure) { failure(error); } }]; } //发送GET请求 +(void)get:(NSString *)url params:(NSDictionary *)params success:(void (^)(id))success failure:(void (^)(NSError *))failure{ AFHTTPRequestOperationManager *mgr = [AFHTTPRequestOperationManager manager]; mgr.responseSerializer = [AFJSONResponseSerializer serializer]; mgr.responseSerializer.acceptableContentTypes = [NSSet setWithObject:@"text/html"]; [mgr GET:url parameters:params success:^(AFHTTPRequestOperation *operation, id responseObject) { if (success) { success(responseObject); } } failure:^(AFHTTPRequestOperation *operation, NSError *error) { failure(error); }]; } //发送POST请求 + (void)post:(NSString *)url params:(NSDictionary *)params success:(void(^)(id responseObjt)) success failure:(void(^)(NSError *error))failure{ AFHTTPRequestOperationManager *mgr = [AFHTTPRequestOperationManager manager]; mgr.responseSerializer = [AFJSONResponseSerializer serializer]; mgr.responseSerializer.acceptableContentTypes = [NSSet setWithObject:@"text/html"]; //发送POST请求 [mgr POST:url parameters:params success:^(AFHTTPRequestOperation *operation, id responseObject) { if (success) { success(responseObject); } } failure:^(AFHTTPRequestOperation *operation, NSError *error) { if (failure) { failure(error); } }]; } +(void)post:(NSString *)url params:(NSDictionary *)params success:(void (^)(id))success failure:(void (^)(NSError *))failure withView:(UIView *)view{ } @end
//使用实例 //NSDictionary *params = @{ // @"userId": userId, // // @"id":_numId // }; // //DLog(@"%@",params); // //[MyURLPost post:confidefaultBankUrl paramters:params success:^(id responseObjt) { // // DLog(@"%@",responseObjt); // // if ([responseObjt[@"resultCode"] isEqualToString:@"100"]) { // [self findCard]; // // }else{ // // } //} failure:^(NSError *error) { // // DLog(@"%@",error); //}];