Question

hi I’m trying check internet connection in my application. so for that i have already imported the reachability h file and m file in project. I’m getting some issues with that now. its working only if internet connection available its not working in without the net connection..

here this is the code which have used..

-(BOOL)reachable {
Reachability *reach = [Reachability reachabilityWithHostName:@"https://www.google.co.in/"];
NetworkStatus internetStatus = [reach currentReachabilityStatus];
if(internetStatus == NotReachable) {

    UIAlertView *alertOne = [[UIAlertView alloc]initWithTitle:@"Internet" message:@"You dont have internet connection to send message" delegate:self cancelButtonTitle:@"cancel" otherButtonTitles:@"ok", nil];
    [alertOne show];
    [alertOne release];


}

    return YES;
}

i have already used this same code in other projects its working their but here its showing the alert message when internet connected its not showing when its not connected ...

this viewdidload code...

[super viewDidLoad];
[self reachable];

pls can any tell me how to resolve this...

thanks

Was it helpful?

Solution

Try below code

Reachability *reach = [Reachability reachabilityForInternetConnection];
//(or)
Reachability *reach = [Reachability reachabilityWithHostName:@"http://www.google.com"];

    NetworkStatus netStatus = [reach currentReachabilityStatus];
    if (netStatus != NotReachable)
    {
        //Reachable ..Network connection is available
    }
    else
    {
        //NSLog(@"Network Error No Network Available ");


        UIAlertView *alertView = [[UIAlertView alloc] initWithTitle:@"" message:@"Please connect to an Internet connection to Register" delegate:nil cancelButtonTitle:@"OK" otherButtonTitles:nil , nil];

        [alertView show];

    }

It works for you...

OTHER TIPS

This may not be the cause of the problem, but it is and always was wrong to call reachable in viewDidLoad. The reason is that viewDidLoad is way too early to call code that might put up an alert view. In viewDidLoad, your view is not even in the interface yet.

Another very odd thing is that your reachable method both detects reachability and puts up the alert. It returns a BOOL but you are throwing it away. You should be saying

if (![self reachable]) { // ...

and putting up the alert view there, not inside the reachable method.

Still another very weird thing is that your reachable method always returns YES. That's nuts. You should return YES if the URL is reachable and NO if it is not. Otherwise, what is the point of returning a BOOL at all?

Please follow this steps,

1>make property of Reachability and NetworkStatus;
in your appDelegate.h

 @interface AppDelegate : UIResponder <UIApplicationDelegate,UITabBarControllerDelegate>{
    Reachability* reachability;
   NetworkStatus remoteHostStatus;

}
@end

2>Create Notification method when reachability changed in applicationDidBecomeActive

  - (void)applicationDidBecomeActive:(UIApplication *)application
{    
    [[NSNotificationCenter defaultCenter] addObserver:self
                                             selector:@selector(reachabilityChanged:)
                                                 name:kReachabilityChangedNotification
                                               object:nil];
    reachability = [Reachability reachabilityForInternetConnection];
    [reachability startNotifier];
    remoteHostStatus = [reachability currentReachabilityStatus];

    if(remoteHostStatus == NotReachable) {
        if(DEBUG_MODE){NSLog(@"no");}

    }
    else if (remoteHostStatus == ReachableViaWiFi) {if(DEBUG_MODE)
    {NSLog(@"wifi");}

    }
    else if (remoteHostStatus == ReachableViaWWAN) {if(DEBUG_MODE){NSLog(@"cell");}

    }

}

3> declare reachabilityChanged

    -(void)reachabilityChanged:(SCNetworkReachabilityFlags)flags
{

 if([self isReachableWithFlags:flags])
    {
        if(self.reachableBlock)
        {
            self.reachableBlock(self);
        }
    }
else
{
    if(self.unreachableBlock)
    {
        self.unreachableBlock(self);
    }
}

// this makes sure the change notification happens on the MAIN THREAD
dispatch_async(dispatch_get_main_queue(), ^{
    [[NSNotificationCenter defaultCenter] postNotificationName:kReachabilityChangedNotification
                                                        object:self];
});

}

Thanks.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top