有几天没有写博客了,今天想写一下网络请求的一点封装,从同事那里看到的,不错的封装吧,不过后半段是我自己的风格;
首先:创建和初始化manager
#import <Foundation/Foundation.h>
#import "GTMObjectSingleton.h"
@interface JXPostManage : NSObject
@property (nonatomic, retain)AFHTTPSessionManager *manager;
-(void)postWithPath:(NSString *)path
params:(NSDictionary *)params
vc:(UIViewController *)vc
success:(void (^)(NSURLSessionTask *task, id responseObject))success
failure:(void (^)(NSURLSessionTask *task, id responseObject))failure;
@end
#import "JXPostManage.h"
#import "AppDelegate.h"
#import "ProgressHUD.h"
@implementation JXPostManage
-(id)init
{
self = [super init];
if (self) {
_manager = [AFHTTPSessionManager manager];
}
return self;
}
-(void)postWithPath:(NSString *)path
params:(NSDictionary *)params
vc:(UIViewController *)vc//VC 为show-->视图 赋值为--->self
success:(void (^)(NSURLSessionTask *task, id responseObject))success
failure:(void (^)(NSURLSessionTask *task, id responseObject))failure
{
NSLog(@"URL -- %@%@",NetPerfix,path);
NSLog(@"请求参数:%@",params);
if (vc != nil) {
[SVProgressHUD showWithStatus:@"载入中..."];
}
NSString *url=[NSString stringWithFormat:@"%@%@",NetPerfix,path];
NSLog(@"请求url......%@",url);
[_manager POST:url
parameters:params
progress:nil
success:^(NSURLSessionDataTask * _Nonnull task, id _Nullable responseObject) {
NSLog(@"errorMessage---%@",responseObject[@"msg"]);
NSLog(@"Path---%@",path);
// if (responseObject[@"resultscode"]) {
// if ([responseObject[@"resultscode"] intValue] == 1) {
// NSLog(@"2");
// success(task,responseObject);
// }else {
// if ([responseObject[@"resultscode"] intValue] == 0) {
// NSLog(@"3");
//
// }else {
// NSLog(@"4");
// NSDictionary *userInfo = [NSDictionary dictionaryWithObjectsAndKeys:
// responseObject[@"errorMessage"], NSLocalizedDescriptionKey, nil];
//
// NSError *error = [NSError errorWithDomain:@"FSB" code:502 userInfo:userInfo];
// failure(task,error);
// }
// }
// }else {
// NSLog(@"5");
// NSDictionary *userInfo = [NSDictionary dictionaryWithObjectsAndKeys:
// @"服务器数据出错", NSLocalizedDescriptionKey, nil];
//
// NSError *error = [NSError errorWithDomain:@"FSB" code:503 userInfo:userInfo];
// failure(task,error);
// }
if ([responseObject[@"resultscode"] intValue] == 2) {
[self isChenckOutWithVC:vc];
[SVProgressHUD dismiss];
return ;
}else{
if ([responseObject[@"resultscode"] intValue] == 1) {
[SVProgressHUD dismiss];
success(task,responseObject);
return ;
}else if([responseObject[@"resultscode"] intValue] == 0){
[self showErrorWithVC:vc withMessag:[NSString stringWithFormat:@"%@",responseObject[@"errorMessage"]]];
[SVProgressHUD dismiss];
return ;
}
}
if (vc != nil)
{
[SVProgressHUD dismiss];
}
} failure:^(NSURLSessionDataTask * _Nullable task, NSError * _Nonnull error) {
NSLog(@"error -- %@",[error localizedDescription]);
if (vc != nil) {
[SVProgressHUD dismiss];
[vc showToast:[error localizedDescription]];
}
failure(task,error);
}];
}
- (void)isChenckOutWithVC:(UIViewController *)vc{
UIAlertController *alert = [UIAlertController alertControllerWithTitle:@"提示" message:@"账号信息已过期或者不存在,请重新登录" preferredStyle:UIAlertControllerStyleAlert];
UIAlertAction *action1 = [UIAlertAction actionWithTitle:@"确认" style:UIAlertActionStyleDefault handler:^(UIAlertAction *action){
[[NSUserDefaults standardUserDefaults] removeObjectForKey:USER_ID_Key];
[[NSUserDefaults standardUserDefaults] removeObjectForKey:@"islogin"];
[[NSUserDefaults standardUserDefaults] removeObjectForKey:USER_TOKEN_KEY];
[[NSUserDefaults standardUserDefaults] removeObjectForKey:@"pwd"];
[[NSUserDefaults standardUserDefaults] removeObjectForKey:USER_PWD];
[[NSUserDefaults standardUserDefaults] removeObjectForKey:@"phone"];
[[NSUserDefaults standardUserDefaults] synchronize];
AppDelegate * app = (AppDelegate *)[UIApplication sharedApplication].delegate;
// [app setLoginRootViewController];
}];
[alert addAction:action1];
[vc presentViewController:alert animated:YES completion:nil];
}
- (void)showErrorWithVC:(UIViewController *)vc withMessag:(NSString *)msg{
UIAlertController *alert = [UIAlertController alertControllerWithTitle:@"提示" message:msg preferredStyle:UIAlertControllerStyleAlert];
UIAlertAction *action0 = [UIAlertAction actionWithTitle:@"确定" style:UIAlertActionStyleDefault handler:nil];
[alert addAction:action0];
[vc presentViewController:alert animated:YES completion:nil];
}
@end
#import "JXPostManage.h"
@interface ZNBNetManager : JXPostManage
+( instancetype ) sharedSingleton;
+(id) allocWithZone:(struct _NSZone *)zone ;
-(id) copyWithZone:(struct _NSZone *)zone ;
//TODO:登录
- (void)vipUserInterWithFlag:(NSString *)flag
type:(NSString *)type
username:(NSString *)username
passwd:(NSString *)passwd
deviceid:(NSString *)deviceid
channelid:(NSString *)channelid
osType:(NSString *)osType
vc:(UIViewController *)vc//VC 为show-->视图 赋值为--->self
success:(void (^)(NSURLSessionTask *task, id responseObject))success
failure:(void (^)(NSURLSessionTask *task, id error))failure;
#import "ZNBNetManager.h"
@implementation ZNBNetManager
static ZNBNetManager *shareSingleton = nil;
+( instancetype ) sharedSingleton {
static dispatch_once_t onceToken;
dispatch_once ( &onceToken, ^ {
shareSingleton = [[super allocWithZone:NULL] init] ;
} );
return shareSingleton;
}
+(id) allocWithZone:(struct _NSZone *)zone {
return [ZNBNetManager sharedSingleton] ;
}
-(id) copyWithZone:(struct _NSZone *)zone {
return [ZNBNetManager sharedSingleton] ;
}
//TODO:登录
- (void)vipUserInterWithFlag:(NSString *)flag
type:(NSString *)type
username:(NSString *)username
passwd:(NSString *)passwd
deviceid:(NSString *)deviceid
channelid:(NSString *)channelid
osType:(NSString *)osType
vc:(UIViewController *)vc//VC 为show-->视图 赋值为--->self
success:(void (^)(NSURLSessionTask *task, id responseObject))success
failure:(void (^)(NSURLSessionTask *task, id error))failure{
NSDictionary *params = [NSDictionary dictionaryWithObjectsAndKeys:
flag,@"flag",
type,@"type",
username,@"username",
passwd,@"passwd",
deviceid,@"deviceid",
channelid,@"channelid",
osType,@"osType",
nil];
[self postWithPath:@"VipUserInter" params:params vc:vc success:success failure:failure];
}