簡體   English   中英

檢查wifi是否打開但沒有互聯網連接

[英]To check wifi is on but no internet connectivity

我使用可達性類來檢查我的代碼中的 wifi 連接。 但有時會出現問題,即 wifi 已打開但沒有或沒有互聯網連接,這里我的代碼循環運行,等待來自調用的 web 服務的任何響應,有時會掛起和崩潰。 當我在從 web 服務中提取一些數據的 AlertView 上單擊 OK 時執行下面的代碼這是我的代碼:

        Reachability *ReachObj = [Reachability reachabilityForInternetConnection];
    [ReachObj startNotifier];
    NetworkStatus remoteHostStatus = [ReachObj currentReachabilityStatus];

    if (remoteHostStatus==ReachableViaWiFi) 
    {
        SecondView *ObjSecView=[[SecondView alloc]init];
        [self presentModalViewController:ObjSecView animated:YES];
    }

    else
        if (remoteHostStatus==NotReachable)
    {
        FirstView *objFrstView=[[FeedBackPopOverViewController alloc]init];
        [self presentModalViewController:objFrstView animated:YES];     
    }

伙計們,我是 Objective C 的新手。 請幫助我,在此先感謝。 並為我的語法錯誤感到抱歉。

嘗試這個..

SCNetworkReachabilityFlags flags;

 SCNetworkReachabilityRef reachability=SCNetworkReachabilityCreateWithName(NULL, [@"your        web sevice url" UTF8String]);

SCNetworkReachabilityGetFlags(reachability, &flags);
BOOL reachable=!(flags & kSCNetworkReachabilityFlagsConnectionRequired);

CFRelease(reachability);
NSURLRequest *request=[NSURLRequest requestWithURL:[NSURL URLWithString:urlString]];


if([NSURLConnection canHandleRequest:request] && reachable)
 { 
conn=[NSURLConnection connectionWithRequest:request delegate:self];

if(conn)
{
    ////        

}
else
{
    [_delegate performSelector:@selector(httpDataDidFailLoadingWithReason:) 
                    withObject:@"No Internet Connection"    afterDelay:0.1];
 }

 }


 -(void) httpDataDidFailLoadingWithReason:(NSString*)reason
 {

UIAlertView *alertView=[[UIAlertView alloc]initWithTitle:@"abc" 
                                                   message:reason
                                                   delegate:self cancelButtonTitle:@"OK"
                                           otherButtonTitles:nil];
[alertView show];
[alertView release];

  }
-(void)loginButtonTouched
{  
    bool success = false;
    const char *host_name = [@"www.google.com" 
                 cStringUsingEncoding:NSASCIIStringEncoding];

    SCNetworkReachabilityRef reachability = SCNetworkReachabilityCreateWithName
                                                    (NULL, host_name);
    SCNetworkReachabilityFlags flags;
    success = SCNetworkReachabilityGetFlags(reachability, &flags);
    bool isAvailable = success && (flags & kSCNetworkFlagsReachable) && 
                       !(flags & kSCNetworkFlagsConnectionRequired);

    if (isAvailable) 
    {
        NSLog(@"Host is reachable: %d", flags);
        // Perform Action if Wifi is reachable and Internet Connectivity is present
    }
    else
    {
        NSLog(@"Host is unreachable");
        // Perform Action if Wifi is reachable and Internet Connectivity is not present
    }       
}

當 loginButtonTouched 方法被調用時,我們檢查“www.google.com”是否可以訪問。 SCNetworkReachabilityFlags 返回標志,幫助我們了解互聯網連接的狀態。 如果 isAvailable 變量返回“true”,則 Host is Reachable 表示 Wifi 可訪問且存在 Internet 連接。

並感謝大家為我們的問題提供快速答案。 對不起,我的語法錯誤。

暫無
暫無

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

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