2

Long time reader, first time asker.

I'm programming an iPhone application that needs to handle the phone going in and out of data coverage with some elegance. I can set up the Reachability with notifications to find out when it gets lost or comes back, but it would be helpful for me to know how often the radios are looking for a signal - and does this rate slow down over time? Also, is there anything I can do programmatically (like pinging a server when I know I don't have coverage) to speed it up?

Battery life is not really a big concern for me, and I will not be deploying through iTunes.

Eimantas
  • 48,927
  • 17
  • 132
  • 168
Alex
  • 21
  • 1
  • Not sure about check decay, but it'd be easy to find out - setup a notifier of getting/losing coverage and check the timestamps of notifications being sent. Regarding manual check - the Apple's Reachability example has all the needed methods for checking access to hosts, ip addresses and other networking entities. – Eimantas Oct 03 '11 at 17:46

1 Answers1

1

What you want is possible. First off get Reachability code from Apple. Then you need to write a checkNetworkStatus implementation. This is where notifications come -

#import "Reachability.h"

- (void)checkNetworkStatus:(NSNotification *)notice
{
    // called after network status changes
    NetworkStatus internetStatus = [internetReachable currentReachabilityStatus];

    switch(internetStatus)
    {
        case NotReachable:
        {
            self.internetActive = NO;
            break;
        }
        case ReachableViaWiFi:
        {
            self.internetActive = YES;
            break;
        }
        case ReachableViaWWAN:
        {
            self.internetActive = YES;
            break;
        }
    }

    NetworkStatus hostStatus = [hostReachable currentReachabilityStatus];
    switch (hostStatus)
    {
        case NotReachable:
        {
            self.hostActive = NO;
            break;
        }
        case ReachableViaWiFi:
        {
            self.hostActive = YES;
            break;
        }
        case ReachableViaWWAN:
        {
            self.hostActive = YES;
            break;
        }
    }
    return;
}

Now you need to start your notifications -

-(void)viewWillAppear:(BOOL)animated
{
    //NSLog(@"View Will Appeared!!");

    // check for internet connection
    [[NSNotificationCenter defaultCenter] addObserver:self 
                                             selector:@selector(checkNetworkStatus:) 
                                                 name:kReachabilityChangedNotification 
                                               object:nil];
    internetReachable = [[Reachability reachabilityForInternetConnection] retain];
    [internetReachable startNotifier];

    // check if a pathway to a random host exists
    hostReachable = [[Reachability reachabilityWithHostName: @"www.google.com"] retain];
    [hostReachable startNotifier];

    // now patiently wait for the notification
    return;
}
Srikar Appalaraju
  • 71,928
  • 54
  • 216
  • 264
  • Yes thank you, but this doesn't really answer the harder part of my question. Do you know if Apple has published (or anyone else has done the work to figure out) how the operating system handles the radios when going in and out of service? I assume they are not continuously scanning for signal due to battery life. There must be an algorithm to determine when they look and for how long they sleep. Thanks again! – Alex Oct 03 '11 at 18:55
  • Not that I know of. I suggest you leave the part of "how the operating system handles the radios when going in and out of service" to apple. Try using the notifications you get & see if that serves your purpose... – Srikar Appalaraju Oct 04 '11 at 03:55
  • @alex now that I can think of maybe you can refer `NSNotificationCenter` to see how often notifications are sent... – Srikar Appalaraju Oct 04 '11 at 03:56
  • @srikar: interesting thought. i dont think it's right because notifications are probably sent much more often than the radios are checked. checking radios is a big hit to battery life. unfortunately, i can't just leave this to apple. it's a bit of a weird application (and one that won't be publicly available -- it's a custom thing for researching rural medicine). thanks though. If you don't mind, I'm going to leave this as unanswered in the hope that someone out there knows the real answer. I appreciate your input. – Alex Oct 13 '11 at 03:35