BOOL isExistenceNetwork = YES;
Reachability *r = [Reachability reachabilityWithHostName:@"www.apple.com"];
switch ([r currentReachabilityStatus]) {
case NotReachable:
isExistenceNetwork=NO;
// NSLog(@"没有网络");
break;
case ReachableViaWWAN:
isExistenceNetwork=YES;
// NSLog(@"正在使用3G网络");
break;
case ReachableViaWiFi:
isExistenceNetwork=YES;
// NSLog(@"正在使用wifi网络");
break;
}
if (!isExistenceNetwork) {
UIAlertView *myalert = [[UIAlertView alloc]
initWithTitle:NSLocalizedString(@"Network error", @"Network error")
message:NSLocalizedString(@"Network isnt connected.Please check.", nil)
delegate:self
cancelButtonTitle:NSLocalizedString(@"Cancel", @"Cancel")
otherButtonTitles:nil];
[myalert show];
[myalert release];
return;
}
需要包含头文件
#import"Reachability.h"
如若未找到此头文件,可以下载开源的网络库ASIHTTP包
Reachability *r = [Reachability reachabilityWithHostName:@"www.apple.com"];
switch ([r currentReachabilityStatus]) {
case NotReachable:
isExistenceNetwork=NO;
// NSLog(@"没有网络");
break;
case ReachableViaWWAN:
isExistenceNetwork=YES;
// NSLog(@"正在使用3G网络");
break;
case ReachableViaWiFi:
isExistenceNetwork=YES;
// NSLog(@"正在使用wifi网络");
break;
}
if (!isExistenceNetwork) {
UIAlertView *myalert = [[UIAlertView alloc]
initWithTitle:NSLocalizedString(@"Network error", @"Network error")
message:NSLocalizedString(@"Network isnt connected.Please check.", nil)
delegate:self
cancelButtonTitle:NSLocalizedString(@"Cancel", @"Cancel")
otherButtonTitles:nil];
[myalert show];
[myalert release];
return;
}
需要包含头文件
#import"Reachability.h"
如若未找到此头文件,可以下载开源的网络库ASIHTTP包