• 关于官方Reachability Demo理解


     

    1. 示例说明:此示例用于演示关于如何使用SystemConfiguratio Reachalility程序接口来判定相关的网络连接状态及状态变化,所以在使用此示例中的Reachability.(h/m)文件于自有的项目中的前提是必须引入SystemConfiguration.framework。

    2. 首选分析Reachability.h文件:

    -> 首先引入<SystemConfiguration/SystemConfiguration.h>

    #import <SystemConfiguration/SystemConfiguration.h>

    -> 定义代表网络状态的枚举类型NetworkStatus:

    复制代码
    // 定义网络请求可到达状态
    typedef enum 
    {
        NotReachable = 0,   // 不可到达
        ReachableViaWiFi,   // 通过WiFi可到达
        ReachableViaWWAN    // 通过无线广域网可到达(WWAN,即Wireless Wide Area Network,无线广域网。)
    } NetworkStatus;
    复制代码

    -> 宏定义关于网络连接变更的通知标识名称:

    #define kReachabilityChangedNotification @"kNetworkReachabilityChangedNotification"

    -> 定义相关属性:

    BOOL localWiFiRef;    
    // 用来保存创建测试连接返回的引用 SCNetworkReachabilityRef reachabilityRef;

    -> 声明各相关的方法:

    // 用于检查网络请求是否可到达指定的主机名
    + (Reachability*) reachabilityWithHostName: (NSString*) hostName;
    // 用于检查网络请求是否可到达指定的IP地址
    + (Reachability*) reachabilityWithAddress: (const struct sockaddr_in*) hostAddress;
    // 用于检查路由连接是否有效
    + (Reachability*) reachabilityForInternetConnection;
    // 用于检查本地的WiFi连接是否有效
    + (Reachability*) reachabilityForLocalWiFi;
    // 在当前程序的运行回路中开始监听网络请求可到达的通知
    - (BOOL) startNotifier;
    - (void) stopNotifier;
    // 当前网络请求可到达状态
    - (NetworkStatus) currentReachabilityStatus;
    // 当前网络请求可到达状态
    - (NetworkStatus) currentReachabilityStatus;
    // 连接需求
    - (BOOL) connectionRequired;
    复制代码
    - (void) applicationDidFinishLaunching: (UIApplication* )application 
    {
    [[NSNotificationCenter defaultCenter]  addObserver:self
                                selector:@selector(reachabilityChanged:)
                                  name:kReachabilityChangedNotification
                                 object:nil];  
    hostReach = [[Reachability reachabilityWithHostName:@"www.apple.com"] retain];
    [hostReach startNotifier];

    internetReach = [[Reachability reachabilityForInternetConnection] retain];
    [internetReach startNotifier];
    [self updateInterfaceWithReachability:internetReach];

    wifiReach = [[Reachability reachabilityForLocalWiFi] retain];
    [wifiReach startNotifier];
    [self updateInterfaceWithReachability:wifiReach];

    [window makeKeyAndVisible]; }

    - (void)reachabilityChanged:(NSNotification *)note
    {
    Reachability *curReach = [note object];
    NSParameterAssert([curReach isKindOfClass:[Reachability class]]);
    [self updateInterfaceWithReachability:curReach];
    }

    - (void)updateInterfaceWithReachability:(Reachability *)curReach
    {
    if (curReach == hostReach)
    {
    [self configureTextField:remoteHostStateField imageView:remoteHostIcon reachability:curReach];
    NetworkStatus netStatus = [curReach currentReachabilityStates];
    BOOL connectionRequired = [curReach connectionRequired];

    summaryLabel.hidden = (netStatus != ReachabilityViaWWAN);
    NSString *baseLabel = @"";
    if (connectionRequired)
    {
    baseLabel = @"Cellular data network is available. Internet traffic will be routed through it after a connection is established.";
    }
    else
         {
    baseLabel = @"Cellular data network is active. Internet traffic will be routed through it.";
    }
    summaryLabel.text = baseLabel;
    }
    if (curReach == internetReach)
    {
    [self configureTextField:internetConnectionStatusField imageView:internetConnectionIcon reachability:curReach];
    }
    if (curReach == wifiReach)
    {
    [self configureTextField:localWiFiConnectionStatusField imageView:localWiFiConnectionIcon reachability:curReach];
    }
    }

    - (void)configureTextField:(UITextField *)textField imageView:(UIImageView *)imageView reachability:(Reachability *)curReach
    {
    NetworkStatus netStatus = [curReach currentReachabilityStatus];
    BOOL connectionRequired = [curReach connectionRequired];
    NSString *statusString = @"";
    switch (netStatus)
    {
    case NotReachable:
    {
    statusString = @"Access Not Available";
    imageView.image = [UIImage imageNamed:@"stop-32.png"];
    connectionRequired = NO;

    break;
    }
    case ReachableViaWWAN:
    {
    statusString = @"Reachable WWAN";
    imageView.image = [UIImage imageNamed:@"WWAN5.png"];

    break;
    }
    case ReachableViaWiFi:
    {
    statusString = @"";
    imageView.image = [UIImage imageNamed:@"Airport.png"];

    break;
    }
    }
    if (connectionRequired)
    {
    statusString = [NSString stringWithFormat:@"%@, Connection Required", statusString];
    }
    textField.text = statusString;
    }
    复制代码

    . 关于Reachability.m

    参考此链接:http://blog.sina.com.cn/s/blog_65a8ab5d010110w5.html

  • 相关阅读:
    天眼查接口的调用
    EditPlus5.0注册码
    图片转化成base64字符串
    java根据图片的url地址下载图片到本地
    将字符串中的数字转化为文字
    String类型转json 转JSONObject 转 JSONArray 以及 遍历
    三个日期类型比较大小 20181123
    字符与字节的区别
    面向对象五大原则
    Struts/Servlet,action转到jsp后,CSS失效,路径问题(struts2,jsp路径,action路径,action跳转,相对路径,绝对路径)
  • 原文地址:https://www.cnblogs.com/YH-Coding/p/Reachability.html
Copyright © 2020-2023  润新知