IOS网络检测


IOS5.1+之后,苹果就删除了程序跳转至设置界面的功能了,不知道为什么。。。

需要导入两个文件: Reachability.h   Reachability.m  .

再导入库文件:SystemConfiguration.framework


所以自己写代码也是不可能实现的,只能够对网络进行监听,然后提醒用户网络链接异常而已。

下面是监听网络改变的代码,可以参考一下:

在AppDelegate.m中写如下代码:

- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{
    self.window = [[[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]] autorelease];
    
    //开启网络状况的监听
    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(reachabilityChanged:) name:kReachabilityChangedNotification object:nil];
    
    self.hostReach = [Reachability reachabilityWithHostName:@"www.baidu.com"] ;
    [self.hostReach startNotifier];  //开始监听,会启动一个run loop

    self.window.rootViewController = self.tabBarController;
    [self.window makeKeyAndVisible];
    return YES;
}

//网络链接改变时会调用的方法
-(void)reachabilityChanged:(NSNotification *)note
{
    Reachability *currReach = [note object];
    NSParameterAssert([currReach isKindOfClass:[Reachability class]]);
    
    //对连接改变做出响应处理动作
    NetworkStatus status = [currReach currentReachabilityStatus];
    //如果没有连接到网络就弹出提醒实况
    self.isReachable = YES;
    if(status == NotReachable)
    {
        UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"网络连接异常" message:@"暂无法访问书城信息" delegate:nil cancelButtonTitle:@"确定" otherButtonTitles:nil];
        [alert show];
        [alert release];
        self.isReachable = NO;
    }
    else
    {
        UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"网络连接信息" message:@"网络连接正常" delegate:nil cancelButtonTitle:@"确定" otherButtonTitles:nil];
        [alert show];
        [alert release];
        self.isReachable = YES;
    }
}



通过如上代码,在应用程序的任何一个界面都可以使用下面的单例来判断网络是否连接

AppDelegate *appDlg = (AppDelegate *)[[UIApplication sharedApplication] delegate];
    if(appDlg.isReachable)
    {
        NSLog(@"网络已连接");//执行网络正常时的代码
    }
    else
    {
        NSLog(@"网络连接异常");//执行网络异常时的代码
    }

然后就可以执行响应的操作了,这样使用监听的好处就是,不必在每一个需要检测网络链接情况的地方都写一大堆代码,只需要上面的监听,网络改变的时候,在任何一个地方都可以自定提醒用户。

监听就是这样滴好用,(*^__^*) 嘻嘻……任何对象都可以接收。

例子2:

//在程序的启动处,开启通知

- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions

{

  //.....

//开启网络状况的监听

[[NSNotificationCenter defaultCenter] addObserver:self

         selector:@selector(reachabilityChanged:)

     name: kReachabilityChangedNotification

   object: nil];

hostReach = [[Reachability reachabilityWithHostName:@"www.google.com"] retain];//可以以多种形式初始化

[hostReach startNotifier];  //开始监听,会启动一个run loop

        [self updateInterfaceWithReachability: hostReach];

  //.....

}


// 连接改变

- (void) reachabilityChanged: (NSNotification* )note

{

Reachability* curReach = [note object];

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

[self updateInterfaceWithReachability: curReach];

}


//处理连接改变后的情况

- (void) updateInterfaceWithReachability: (Reachability*) curReach

{

    //对连接改变做出响应的处理动作。

        NetworkStatus status = [curReach currentReachabilityStatus];

    

if (status == NotReachable) {  //没有连接到网络就弹出提实况

UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"My App Name"

                              message:@"NotReachable"

                              delegate:nil

                              cancelButtonTitle:@"YES" otherButtonTitles:nil];

                              [alert show];

                              [alert release];

}

}

你可能感兴趣的:(IOS网络检测)