[英]Unexpected result when checking network reachability
In my app I implemented the code for network reachability. 在我的应用中,我实现了网络可达性代码。 This is my code.
这是我的代码。
-(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;
}
This code returns me gotInternet = NO; 这段代码使我得到gotInternet = NO; Actually WiFi is enabled.
实际上启用了WiFi。 What am i doing wrong?
我究竟做错了什么?
Thank you in advance. 先感谢您。
Your problem is you are testing a FULL URL not a HOST NAME..... 您的问题是您要测试的是完整URL而不是主机名。
Reachability *r = [Reachability reachabilityWithHostName:@"mobile.areafinancial.com"];
NetworkStatus internetStatus = [r currentReachabilityStatus];
try this I have modified your code 试试这个我已经修改了你的代码
-(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;
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.