在我的应用中,我实现了网络可达性代码。 这是我的代码。
-(void)viewDidLoad {
// Test for internet.
gotInternet = [self checkInternet];
if (gotInternet == NO) {
// No Internet.
UIAlertView *alert = [[UIAlertView alloc]initWithTitle:@"Connection Error" message:@"The internet is down" delegate:self cancelButtonTitle:@"OK" otherButtonTitles:nil, nil];
[alert show];
[alert release];
}
else {
NSLog(@"I do have internet");
}
}
-(BOOL)checkInternet {
//Test for Internet Connection
NSLog(@"——–");
NSLog(@"Testing Internet Connectivity");
Reachability *r = [Reachability reachabilityWithHostName:@"https://mobile.areafinancial.com/cutechservice/cutechwebservice.asmx"];
NetworkStatus internetStatus = [r currentReachabilityStatus];
BOOL internet;
if ((internetStatus != ReachableViaWiFi) && (internetStatus != ReachableViaWWAN)) {
internet = NO;
} else {
internet = YES;
}
return internet;
}
此代码返回给我gotInternet = NO;实际上WiFi已启用。我做错了什么?
提前谢谢。
答案 0 :(得分:0)
您的问题是您正在测试一个完整的网址而不是主机名.....
Reachability *r = [Reachability reachabilityWithHostName:@"mobile.areafinancial.com"];
NetworkStatus internetStatus = [r currentReachabilityStatus];
答案 1 :(得分:-1)
试试这个我修改了你的代码
-(void)viewDidLoad {
// Test for internet.
//gotInternet = [self checkInternet];
if (![self checkInternet]) {
// No Internet.
UIAlertView *alert = [[UIAlertView alloc]initWithTitle:@"Connection Error" message:@"The internet is down" delegate:self cancelButtonTitle:@"OK" otherButtonTitles:nil, nil];
[alert show];
[alert release];
}
else {
NSLog(@"I do have internet");
}
}
-(BOOL)checkInternet {
//Test for Internet Connection
NSLog(@"——–");
NSLog(@"Testing Internet Connectivity");
Reachability *r = [Reachability reachabilityWithHostName:@"https://mobile.areafinancial.com/cutechservice/cutechwebservice.asmx"];
NetworkStatus internetStatus = [r currentReachabilityStatus];
BOOL internet;
if ((internetStatus != ReachableViaWiFi) && (internetStatus != ReachableViaWWAN)) {
return NO;
} else {
return YES;
}
//return internet;
}