简体   繁体   中英

Reachability AlertView in iOS Programing

So I make alert view detects if there in a active internet connection.

This is the Code:

- (void)viewDidLoad
{
    [super viewDidLoad];

    [[NSNotificationCenter defaultCenter] addObserver:self
    selector:@selector(reachabilityChanged:)
    name:kReachabilityChangedNotification
    object:nil];
    Reachability * reach = [Reachability reachabilityWithHostname:@"www.google.com"];
    reach.reachableBlock = ^(Reachability * reachability)
    { 
     dispatch_async(dispatch_get_main_queue(), ^{
     blockLabel.text = @"";  
     });
    };
    reach.unreachableBlock = ^(Reachability * reachability)
    {
     dispatch_async(dispatch_get_main_queue(), ^{
     blockLabel.text = @"You are not connected to the Internet";
     UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"Please connect to Internet"
     message:nil
     delegate:self
     cancelButtonTitle:nil
     otherButtonTitles:nil];
     UIActivityIndicatorView *progress= [[UIActivityIndicatorView alloc] initWithFrame:CGRectMake(125, 50, 30, 30)];
     progress.activityIndicatorViewStyle = UIActivityIndicatorViewStyleWhiteLarge;
            [alert addSubview:progress];
            [progress startAnimating];
            [alert show]; 
        });
    };
    [reach startNotifier];
    // Do any additional setup after loading the view, typically from a nib.
}

So my application detects if there is a internet connection. But the issue is that if I turn on internet on the iPhone and open the application it still saying there is not internet connection. What should I do…

Well i fix the issue by using notifications.. Code for Reference

-(void)reachabilityChanged:(NSNotification*)note
{
    Reachability * reach = [note object];

    if([reach isReachable])
    {
        notificationLabel.text = @"Notification Says Reachable";
        NSLog(@"Internet is Up");
    }
    else
    {
        notificationLabel.text = @"Notification Says Unreachable";
        NSLog(@"Internet is Down");
    }
}

You could always implement a non-stop checking for host reachability (or internet connectivity) by signing up for reachability notifications.

One of best examples on how to do this can be found here on SO: How to check for an active Internet Connection on iPhone SDK?

This way your app will always know if the host is reachable or not.

There is no telling what your sparse code does as it does not even look like your Reachability class is Apple's Reachability sample code wrapper around the SystemConfiguration API , you can find that at http://developer.apple.com/library/ios/#samplecode/Reachability/Introduction/Intro.html

The simplest and most definitive check is just a uncached NSURLRequest for a specific url (not just a hostname which merely tests if DNS resolution is working which is not a definitive test and could even be cached)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM