iOS检测网络是否断开

//先添加Reachability.h 和 Reachability.m类

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

{

    //判断网络是否断开

    [[NSNotificationCenter defaultCenteraddObserver:self

                                            selector:@selector(notificationcenter:)

                                                name:kReachabilityChangedNotification

                                               object:nil];

    

    Reachability * reach = [Reachability reachabilityWithHostname:@"www.baidu.com"];

    [reach startNotifier];

    [self.window makeKeyAndVisible];

    return YES;

}

//通知

-(void)notificationcenter:(NSNotification*)note

{

   Reachability * reach = [note object];

    if([reach isReachable])

    {

        NSLog(@"Reachable");

    }

    else

    {

        UIAlertView *alertView = [[UIAlertView allocinitWithTitle:nil message:@"网络已断开" delegate:nil cancelButtonTitle:@"确定" otherButtonTitles:nil];

        [alertView show];

        NSLog(@"Unreachable");

    }

}

你可能感兴趣的:(ios,检测网络,iOS检测网络是否断开)