簡體   English   中英

檢查網絡可達性時出現意外結果

[英]Unexpected result when checking network reachability

在我的應用中,我實現了網絡可達性代碼。 這是我的代碼。

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

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM