美文网首页
用AFN做网络判断

用AFN做网络判断

作者: 英贰与我 | 来源:发表于2015-08-29 16:44 被阅读3071次

    //创建一个基于AFHTTPSessionManager 的类

    #import <Foundation/Foundation>

    #import "AFHTTPSessionManager.h"

    @interfaceAFAppDotNetAPIClient :AFHTTPSessionManager

    //创建单例,在需要网络判断的地方调用

    + (instancetype)sharedClient;

    @end

    #import"AFAppDotNetAPIClient.h"

    //全局静态变量 是测试网络用到的网址

    static NSString*const AFAppDotNetAPIBaseURLString =@"https://api.app.net/";

    @implementationAFAppDotNetAPIClient

    //实现单例方法

    + (instancetype)sharedClient {

    staticAFAppDotNetAPIClient*_sharedClient =nil;

    //用dispatch_once 函数 解决线程问题 防止多线程时被不同线程同时调用  类似加锁的功能

    staticdispatch_once_tonceToken;

    dispatch_once(&onceToken, ^{

    _sharedClient = [[AFAppDotNetAPIClientalloc]initWithBaseURL:[NSURLURLWithString:AFAppDotNetAPIBaseURLString]];

    _sharedClient.securityPolicy= [AFSecurityPolicypolicyWithPinningMode:AFSSLPinningModeNone];

    //网络判断的内部实现        [_sharedClient.reachabilityManagersetReachabilityStatusChangeBlock:^(AFNetworkReachabilityStatusstatus) {

    switch(status) {

    caseAFNetworkReachabilityStatusReachableViaWWAN:

    NSLog(@"-------AFNetworkReachabilityStatusReachableViaWWAN------");

    break;

    caseAFNetworkReachabilityStatusReachableViaWiFi:

    NSLog(@"-------AFNetworkReachabilityStatusReachableViaWiFi------");

    break;

    caseAFNetworkReachabilityStatusNotReachable:

    NSLog(@"-------AFNetworkReachabilityStatusNotReachable------");

    break;

    default:

    break;

    }

    }];

    [_sharedClient.reachabilityManagerstartMonitoring];

    });

    return_sharedClient;

    }

    //用if判断网络状态 增加了自动提示---提示框

    if(status ==

    AFNetworkReachabilityStatusReachableViaWWAN) {

    UIAlertView*alertView = [[UIAlertViewalloc]initWithTitle:@"温馨提示"message:@"目前3G网络状态良好"delegate:selfcancelButtonTitle:nilotherButtonTitles:nil,nil];

    [alertViewshow];

    alertView.cancelButtonIndex=1;

    //让alertView自动取消这里的self是指shareClient

    [selfperformSelector:@selector(dissAlertView:)withObject:alertViewafterDelay:1.0];

    //WiFi

    }elseif(status ==AFNetworkReachabilityStatusReachableViaWiFi){

    UIAlertView*alertView = [[UIAlertViewalloc]initWithTitle:@"温馨提示"message:@"目前是WiFi网络"delegate:selfcancelButtonTitle:nil

    otherButtonTitles:nil,nil];

    [alertViewshow];

    //让alertView自动取消

    [selfperformSelector:@selector(dissAlertView:)withObject:alertViewafterDelay:1.0];

    //无网络

    }elseif(status ==AFNetworkReachabilityStatusNotReachable){

    UIAlertView*alertView = [[UIAlertViewalloc]initWithTitle:@"温馨提示"message:@"无法找到网络"delegate:selfcancelButtonTitle:nilotherButtonTitles:nil,nil];

    [alertViewshow];

    [selfperformSelector:@selector(dissAlertView:)withObject:alertViewafterDelay:1.0];

    }

    }];

    [shareClient.reachabilityManagerstartMonitoring];

    });

    returnshareClient;

    }

    //在单例里面 这个方法必须写为加号方法

    因为在上面的网络判断中 我们在return 前就调用了这个方法,而调用时实例还没有产生,所以用减号方法会崩溃的 ;

    //alertView自动消失的实现方法

    + (void)dissAlertView:(UIAlertView*)alertView

    {

    if(alertView) {

    [alertViewdismissWithClickedButtonIndex:[alertViewcancelButtonIndex]animated:YES];

    }

    [alertViewrelease];

    }

    相关文章

      网友评论

          本文标题:用AFN做网络判断

          本文链接:https://www.haomeiwen.com/subject/vwjwqttx.html