IOS网络——检测网络状态:Reachability

    xiaoxiao2021-11-30  21

    转自:http://www.bubuko.com/infodetail-650914.html

    1.iOS平台是按照一直有网络连接的思路来设计的,开发者利用这一特点创造了很多优秀的第三方应用。大多数的iOS应用都需要联网,甚至有些应用严重依赖网络,没有网络就无法正常工作。

    2.在你的应用尝试通过网络获取数据之前,你需要知道当前设备是否知道连接上了网络,甚至有时候你可能还需要知道当前网路是由wifi还是由移动蜂窝网络提供的。

    3.“在网络访问失败的时候,应用没有做出适当的提示”是苹果的iOS审核团队拒绝一个应用的常见理由。苹果要求你必须先检测网络连接状态,当网络不可用的时候以某种方式告知用户,或者用其他优雅的方式进行处理。

     

    ***********************

    Reachability类:

    1.这个类用于检测当前网络状态,它不是SDK的一部分,可以在iOS Developer Library里找到这份代码。

    从苹果网站上下载Reachability.zip文件,解压之。

    2.重用Reachability类

        (1)把Reachability.h和Reachability.m文件拖到项目中。

        (2)添加框架:SystemConfiguration.framework。

    3.同步的Reachability

        (1)使用同步的方式是比较简单,导入Reachability.h头文件,然后通过代码检查网络:

            #import “Reachability.h”

            。。。some code omitted…

            Reachability *reach = [Reachability reachabilityForInternetConnection];

            NetworkStatus status = [reach currentReachabilityStatus];

         (2)通过检查某个主机能否访问来判断当前网络是否可用:

            Reachability *reach = [Reachability reachabilityWithHostName:@“www.apple.com”];

            NetworkStatus status = [reach currentReachabilityStatus];

         (3)案例:

            创建一个工程,并添加Reachability.h和Reachability.m到工程中,并链接SystemConfiguration.framework.

            在AppDelegate.h头文件中导入Reachability.h,并添加一个实例方法。如图:

                    

            在AppDelegate.m中这样实现:如图:

                

            

    4.异步的Reachability

        (1)异步的方式稍微复杂,不过通过这种方式可以来订阅实时的网络状态变化通知。导入Reachability.h头文件,然后注册一个对象来订阅网络状态变化的信息,网络状态变化的信息名称为kReachabilityChanged-Notification.如下:

        [[NSNotificationCenter defaultCenter] addObserver:self

            selector:@selector(reachabilityChanged:)

            name:kReachabilityChangedNotification

            object:nil];

        (2)你需要创建一个Reachability对象实例并开始向外发布网络状态变化的消息://注意要创建一个属性

        @property (nonatomicReachability *internetReachability;

        self.internetReachability = [Reachability reachabilityForInternetConnection];

        [self.internetReachability startNotifier];

        [self reachability:self.internetReachability];

        (3)当网络状态发生变化的时候,Reachability对象将调用reachabilityChanged:方法,可以在这个方法里面获取当前的网络状态,然后做相应的处理。

    #pragma mark - reachabilityChanged

    /*!

     * Called by Reachability whenever status changes.

     */

    - (void) reachabilityChanged:(NSNotification *)note

    {

        Reachability* curReach = [note object];

        NSParameterAssert([curReach isKindOfClass:[Reachability class]]);

        [self reachability:curReach];

    }

    - (void)reachability:(Reachability *)reachability

    {

        NetworkStatus netStatus = [reachability currentReachabilityStatus];

        BOOL connectionRequired = [reachability connectionRequired];

        NSString* statusString = @"";

        

        switch (netStatus)

        {

            case NotReachable:        {

                statusString = NSLocalizedString(@"Access Not Available"@"Text field text for access is not available");

                /*

                 Minor interface detail- connectionRequired may return YES even when the host is unreachable. We cover that up here...

                 */

                connectionRequired = NO;

                break;

            }

                

            case ReachableViaWWAN:        {

                statusString = NSLocalizedString(@"Reachable WWAN"@"");

                break;

            }

            case ReachableViaWiFi:        {

                statusString= NSLocalizedString(@"Reachable WiFi"@"");

                break;

            }

        }

        

        if (connectionRequired)

        {

            NSString *connectionRequiredFormatString = NSLocalizedString(@"%@, Connection Required",@"Concatenation of status string with connection requirement");

            statusString= [NSString stringWithFormat:connectionRequiredFormatString, statusString];

        }

        

        NSLog(@"connection status = [%@]",statusString);

    }

    5.原生 Reachability API

    前面将的Reachability类实际上是苹果公司对SCNetworkReachability API的封装,这个API定义在SystemConfigure.framework库中。如果有其他特别的需求,也可以直接使用这个原生的SCNetworkReachability类。

    转载请注明原文地址: https://ju.6miu.com/read-679161.html

    最新回复(0)