开发者

To check wifi is on but no internet connectivity

开发者 https://www.devze.com 2023-03-20 07:37 出处:网络
i m using reachability classes to check wifi connectivity in my code. But sometimes problem arise that wifi is ON but there is no or low internet connectivity, here my code runs in loop waiting for an

i m using reachability classes to check wifi connectivity in my code. But sometimes problem arise that wifi is ON but there is no or low internet connectivity, here my code runs in loop waiting for any response from called webservice and hangsup and crashes sometimes. Below Code is executed when i hit OK on an AlertView which pulls some data from webservice Here is my code :

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

Guys i m new to Objective C. Plz help me out, thanks in advance. And sorry for my grammatical mistakes.


try this..

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

When loginButtonTouched method is called we check that "www.google.com" is reachable or not. SCNetworkReachabilityFlags returns flags which helps us to understand the Status of internet connectivity. If isAvailable variable returns "true" then Host is Reachable means Wifi is reachable and Internet Connectivity is present.

And thanks to all for providing with your quick answers to our questions. Sorry all for my grammatical mistakes.

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号