//先添加Reachability.h 和 Reachability.m类
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{
//判断网络是否断开
[[NSNotificationCenter defaultCenter] addObserver:self
selector:@selector(notificationcenter:)
name:kReachabilityChangedNotification
object:nil];
Reachability * reach = [Reachability reachabilityWithHostname:@"www.baidu.com"];
[reach startNotifier];
[self.window makeKeyAndVisible];
return YES;
}
//通知
-(void)notificationcenter:(NSNotification*)note
{
Reachability * reach = [note object];
if([reach isReachable])
{
NSLog(@"Reachable");
}
else
{
UIAlertView *alertView = [[UIAlertView alloc] initWithTitle:nil message:@"网络已断开" delegate:nil cancelButtonTitle:@"确定" otherButtonTitles:nil];
[alertView show];
NSLog(@"Unreachable");
}
}