Apple 的 例程 Reachability 中介绍了取得/检测网络状态的方法。
1.在你的程序中使用 Reachability 只须将该例程中的 Reachability.h 和 Reachability.m 拷贝到你的工程中。
2.然后将 SystemConfiguration.framework 添加进工程。
我使用的版本为 : Version: 2.2
我为Apple的例程增加了一个全局 -- ReachabilityAutoChecker
.h
@interface ReachabilityAutoChecker : NSObject @property (nonatomic, retain) Reachability *reachability; @property (nonatomic, assign) NetworkStatus networkStatus; @property (nonatomic, assign) BOOL connectionRequired; @end
.m文件
@implementation ReachabilityAutoChecker
@synthesize reachability;
+ (id)sharedChecker
{
static ReachabilityAutoChecker *staticChecker = nil;
if (!staticChecker) {
staticChecker = [[ReachabilityAutoChecker alloc] init];
[[NSNotificationCenter defaultCenter] addObserver:staticChecker selector:@selector(reachabilityChanged:) name:kReachabilityChangedNotification object:nil];
staticChecker.networkStatus = NotReachable;
staticChecker.connectionRequired = NO;
}
return staticChecker;
}
- (void)reachabilityChanged:(NSNotification* )note
{
Reachability* curReach = [note object];
NSParameterAssert([curReach isKindOfClass: [Reachability class]]);
self.networkStatus = [curReach currentReachabilityStatus];
self.connectionRequired = [curReach connectionRequired];
}
@end
我为Apple的例程增加了一个Category -- Reachability (AutoChecker)
.h文件:
@interface Reachability (AutoChecker) + (void)startCheckWithReachability:(Reachability *)reachability; + (BOOL)isReachable; @end
.m文件:
@implementation Reachability (AutoChecker)
+ (void)startCheckWithReachability:(Reachability *)reachability
{
ReachabilityAutoChecker *checker = [ReachabilityAutoChecker sharedChecker];
if (checker.reachability) {
[checker.reachability stopNotifier];
checker.reachability = nil;
}
checker.reachability = reachability;
[checker.reachability startNotifier];
}
+ (BOOL)isReachable
{
ReachabilityAutoChecker *checker = [ReachabilityAutoChecker sharedChecker];
if (!checker.reachability) {
NSLog(@"Check Reachability With No Reachability has been Set!");
return NO;
}
NetworkStatus networkStatus = [checker networkStatus];
if(networkStatus == ReachableViaWiFi)
{
NSLog(@"WIFI");
}
if(networkStatus == ReachableViaWWAN)
{
NSLog(@"3G");
}
BOOL connectionRequired = NO;
connectionRequired = [checker connectionRequired];
#if kShouldPrintReachabilityFlags
NSLog(@"NetworkStatus %d connectionRequired %d", networkStatus, connectionRequired);
#endif
if (networkStatus)
return YES;
else
return NO;
}
@end
调用方式:
AppDelegate.m
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{
//检测某一特定站点的接续状况,可以使用下面的代码
Reachability *pReachability = [Reachability reachabilityWithHostName:@"appservices.comcsoft.com"];
//开始监控网络状态
[Reachability startCheckWithReachability:pReachability];
self.window = [[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]];
// Override point for customization after application launch.
self.viewController = [[ViewController alloc] initWithNibName:@"ViewController" bundle:nil];
self.window.rootViewController = self.viewController;
[self.window makeKeyAndVisible];
return YES;
}
ViewController.m
- (IBAction)upInside_checkNetStatus:(id)sender
{
if(![Reachability isReachable])
{
UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"提示"
message:@"对不起,网络异常,请检查您的网络设置。"
delegate:self
cancelButtonTitle:@"好的"
otherButtonTitles:nil];
[alert show];
return;
}
}
运行时提醒下 : #error "此类需要在非arc环境下编译,请添加-fno-objc-arc标记"
网络正常 NSLog如下:
2013-07-05 14:15:53.084 PRJ_reachability[8153:11303] Reachability Flag Status: -R ------- networkStatusForFlags
2013-07-05 14:15:54.265 PRJ_reachability[8153:11303] WIFI
2013-07-05 14:15:54.266 PRJ_reachability[8153:11303] NetworkStatus 1 connectionRequired 0
IOS Reachability判断所请求服务器是否超时?(转)
原文:http://www.cnblogs.com/pjl111/p/4230744.html