AFNetworking封装

封装改了好多次,一直没更新这里

新增 -取消请求
-网络监听
-https证书

1.手机网络监听
苹果官方有提供一个 Reachablity 来检测用户网络情况, 但是项目中如果导入 AFNetworking 的话,就没必要再用 Reachablity 了.

pragma mark - 开启手机网络的监听

  • (void)startNotificationNetworkStatus
    {
    [NetWork shareInstance].currentNetworkStatus = NO;

    AFNetworkReachabilityManager *networkStatusManage = [AFNetworkReachabilityManager sharedManager];
    [networkStatusManage setReachabilityStatusChangeBlock:^(AFNetworkReachabilityStatus status) {
    switch (status) {
    case AFNetworkReachabilityStatusUnknown:
    NSLog(@"未识别的网络");
    [NetWork shareInstance].currentNetworkStatus = NO;
    [NetWork shareInstance].netStatus = NetworkStatusNoNet;
    break;
    case AFNetworkReachabilityStatusNotReachable:
    NSLog(@"网络连接已断开");
    [NetWork shareInstance].currentNetworkStatus = NO;
    [NetWork shareInstance].netStatus = NetworkStatusNoNet;
    break;
    case AFNetworkReachabilityStatusReachableViaWWAN:
    NSLog(@"当前网络为移动网络");
    [NetWork shareInstance].currentNetworkStatus = YES;
    [NetWork shareInstance].netStatus = NetworkStatusMobile;
    break;
    case AFNetworkReachabilityStatusReachableViaWiFi:
    NSLog(@"当前网络为Wifi网络");
    [NetWork shareInstance].currentNetworkStatus = YES;
    [NetWork shareInstance].netStatus = NetworkStatusWifi;
    break;
    default:
    break;
    }
    }];
    [networkStatusManage startMonitoring];
    }

pragma mark - 返回当前网络是否可用

  • (BOOL)getCurrentNetworkStatus
    {
    return [NetWork shareInstance].currentNetworkStatus;
    }

pragma mark - 返回当前的网络状态

  • (NetworkStatus)returnCurrentNetworkStatus
    {
    return [NetWork shareInstance].netStatus;
    }

2.取消请求
在NSURLSession开发中,所有的任务都是由session发起的
AFNetworking 3.0 之后,都是基于 NSURLSession
AF中调用请求 会返回一个 NSURLSessionDataTask 对象
可以先把这个对象添加到队列,当然,需要先创建一个队列管理的容器

pragma mark - 队列管理容器

  • (NSMutableDictionary *)taskQueue
    {
    NSMutableDictionary * taskDic = objc_getAssociatedObject(self, @selector(addTask:));
    if (!taskDic) {
    taskDic = @{}.mutableCopy;
    objc_setAssociatedObject(self, @selector(addTask:), taskDic, OBJC_ASSOCIATION_RETAIN_NONATOMIC);
    }
    return taskDic;
    }

pragma mark - 添加队列

  • (void)addTask:(NSURLSessionDataTask *)task
    {
    NSMutableDictionary * taskQueue = [self taskQueue];
    [taskQueue setObject:task forKey:@(task.taskIdentifier)];
    }

请求返回结果后,需要从队列移除.删除的时候,最好先判断是否存在队列

pragma mark - 判断有没有执行中的队列

  • (BOOL)isRequesting
    {
    NSMutableDictionary * taskDic = objc_getAssociatedObject(self, @selector(addTask:));
    if (taskDic.allValues.count > 0) {
    return YES;
    }
    return NO;
    }

pragma mark - 删除队列

  • (void)removeTask:(NSURLSessionDataTask *)task
    {
    if ([self isRequesting]) {
    NSMutableDictionary * taskQueue = [self taskQueue];
    [taskQueue removeObjectForKey:@(task.taskIdentifier)];
    }
    }

pragma mark - 取消请求

  • (void)cancelRequest
    {
    if ([self isRequesting]) {
    NSMutableDictionary * taskQueue = [self taskQueue];
    [taskQueue enumerateKeysAndObjectsUsingBlock:^(id _Nonnull key, id _Nonnull obj, BOOL * _Nonnull stop) {
    if (((NSURLSessionDataTask *)obj).state != NSURLSessionTaskStateCompleted ) {
    [((NSURLSessionDataTask *)obj) cancel];
    }
    }];

    }
    }

3.苹果要求必须https,需要自签名证书
其实用AF的话很简单

pragma mark - 设置证书

  • (AFSecurityPolicy*)customSecurityPolicy
    {
    NSString *cerPath = [[NSBundle mainBundle] pathForResource:@"证书名" ofType:@"cer"];
    NSData *certData = [NSData dataWithContentsOfFile:cerPath];
    AFSecurityPolicy *securityPolicy = [AFSecurityPolicy policyWithPinningMode:AFSSLPinningModeCertificate];
    securityPolicy.allowInvalidCertificates = YES;
    securityPolicy.validatesDomainName = NO;
    securityPolicy.pinnedCertificates = [[NSSet alloc] initWithObjects:certData,nil];
    return securityPolicy;
    }

附上一个简单demo:https://github.com/zhen-li-xing/LZAFRequest.git

你可能感兴趣的:(AFNetworking封装)