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 anyt开发者_运维知识库hing 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.
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;
}
精彩评论