ios检测网络状态Reachability

本文介绍如何使用苹果提供的Reachability类检测iOS设备的网络状态。Reachability类可通过同步或异步方式检查网络连接情况,并监听网络状态变化。此外,还介绍了如何利用SCNetworkReachability API进行更深层次的定制。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

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,并添加一个实例方法。
#import <UIKit/UIKit.h>
#import "Reachability.h"

@interface AppDelegate : UIResponder <UIApplicationDelegate>

@property (strong, nonatomic) UIWindow *window;
- (NSString *)stringFromStatus:(NetworkStatus)status;

@end

在AppDelegate.m中这样实现:

@implementation AppDelegate


- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
    Reachability *reach = [Reachability reachabilityForInternetConnection];
    NetworkStatus status = [reach currentReachabilityStatus];
    UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"Reachability" message:[self stringFromStatus:status] delegate:nil cancelButtonTitle:@"ok" otherButtonTitles:nil];
    [alert show];
    return YES;
}

- (NSString *)stringFromStatus:(NetworkStatus)status{
    NSString *string;
    switch (status) {
        case NotReachable:
            string = @"网络故障";
            break;
        case ReachableViaWiFi:
            string = @"wifi链接";
            break;
        case ReachableViaWWAN:
            string = @"wwan";
            break;

        default:
            string = @"unknown";
            break;
    }

    return string;
}

4.异步的Reachability

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

[[NSNotificationCenter defaultCenter] addObserver:self

    selector:@selector(reachabilityChanged:)

    name:kReachabilityChangedNotification

    object:nil];

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

//这里的reach必须是strong类型的成员引用,切记!
self.reach = [Reachability reachabilityWithHostName:@“www.apple.com”];

    [reach startNotifier];

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

    - (void)reachabilityChanged:(NSNotification *)notification{

        Reachability *reach = [notification object];

        if([reach isKindOfClass:[Reachability class]]){

            NetworkStatus status = [reach currentReachabilityStatus];

            //Insert your code here

        }                   

    }
    (4)例子:
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
    [[NSNotificationCenter defaultCenter] addObserver:self
                                             selector:@selector(reachabilityChanged:)
                                                 name:kReachabilityChangedNotification
                                               object:nil];
    self.hostReach = [Reachability reachabilityWithHostName:@"www.19500.com"];
    [self.hostReach startNotifier];
    NSLog(@"how are you");
    return YES;
}

- (void)reachabilityChanged:(NSNotification*)note
{
    NSLog(@"haha");
    Reachability* curReach = [note object];
    NSParameterAssert([curReach isKindOfClass:[Reachability class]]);
    NetworkStatus status = [curReach currentReachabilityStatus];

    if (status == NotReachable) {
        UIAlertView* alert = [[UIAlertView alloc] initWithTitle:@"19500彩票网"
                                                        message:@"没有网络连接,请检查网络"
                                                       delegate:nil
                                              cancelButtonTitle:@"确定"
                                              otherButtonTitles:nil];
        alert.tag = 9080;
        alert.delegate = self;
        [alert show];
    }
}

5.原生 Reachability API

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

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值