检查网络可达性时出现意外结果



在我的应用程序中,我实现了网络可达性代码。这是我的代码。

-(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是开启的。我做错了什么?

提前感谢。

您的问题是您正在测试一个完整的URL而不是主机名.....

    Reachability *r = [Reachability reachabilityWithHostName:@"mobile.areafinancial.com"];
    NetworkStatus internetStatus = [r currentReachabilityStatus];

试试我已经修改了你的代码

-(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;
}

最新更新