0

in my app i want to show an alert message when user has no internet connection i used the below code but it never enters to this block it never fires it never called

how can i control if user has internet connection or not?

-(void)connection:(NSURLConnection *)connection didFailWithError:(NSError *)error
{
    NSLog(@"connect");
    UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"Connection Failed" message:@"check your internet connection" delegate:self cancelButtonTitle:@"OK" otherButtonTitles:nil, nil];
    [alert show];

}

note: i use iOS 5.1 version and i have put my delegates in to the .h file

ercan
  • 825
  • 3
  • 16
  • 27
  • make sure this method is in the delegate of your NSURLConnection – Vatev Jul 22 '12 at 14:44
  • i have the delegates: NSURLConnectionDataDelegate,NSURLConnectionDelegate – ercan Jul 22 '12 at 14:49
  • https://developer.apple.com/library/mac/#documentation/Foundation/Reference/NSURLConnectionDelegate_Protocol/Reference/Reference.html it is in the NSURLConnectionDelegate. – hkulekci Jul 22 '12 at 14:49

1 Answers1

2

Try Reachability Class to check availability of Internet connection. It is much faster than connection:(NSURLConnection *)connection didFailWithError Usually it will take 1 minute to fire this method when there is no Internet connection. But the Reachability block will fire as soon as the connection switches.

rakeshNS
  • 4,227
  • 4
  • 28
  • 42
  • thanks, i tried to using Reachability class. i add the .m and .h files then how can i use it in my xib file? in viewDidLoad how can i use the Reachability Class to check the availability? – ercan Jul 22 '12 at 15:19
  • see this post. http://stackoverflow.com/questions/3790957/reachability-guide-for-ios-4 – rakeshNS Jul 22 '12 at 15:22