IOS 判断网络连接状态 Reachability

首先得下载Reachability文件并且加入到工程中

if([[Reachability reachabilityForLocalWiFi] currentReachabilityStatus]!=NotReachable)
    {
        NSLog(@"wifi is ok");
    }
    else
    {
        NSLog(@"wifi is close");
//        UIAlertView *alertView=[[UIAlertView alloc] initWithTitle:@"提示" message:@"网络连接错误" delegate:self cancelButtonTitle:@"确认" otherButtonTitles:nil, nil];
//        [alertView show];
//        [alertView release];
    }
    
    if([[Reachability reachabilityForInternetConnection] currentReachabilityStatus]!=NotReachable)
    {
        NSLog(@"3G is ok");
    }
    else
    {
        NSLog(@"3G is close");
    }

-(void)alertView:(UIAlertView*)alertView clickedButtonAtIndex:(NSInteger)buttonIndex
{
    NSLog(@"alertview click");
    switch (buttonIndex) {
        case 0:
            NSLog(@"click ok button");
            exit(0);
            break;
            
        default:
            break;
    }
}


你可能感兴趣的:(IOS 判断网络连接状态 Reachability)