标签:网络
#import "ViewController.h"
#import "Reachability.h"
#import "Common.h"
@interface ViewController ()
{
NetworkStatus netstatus;
BOOL isConnected;
Reachability * hostReach;
}
@end
@implementation ViewController
- (void)viewDidLoad {
[super viewDidLoad];
// Do any additional setup after loading the view, typically from a nib.
}
- (void)didReceiveMemoryWarning {
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
}
- (IBAction)butonClick:(id)sender {
[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(reachabilityChanged:) name:kReachabilityChangedNotification object:nil];
hostReach = [Reachability reachabilityWithHostName:@"www.baidu.com"];
[hostReach startNotifier];
}
// 网络监听事件
-(void)reachabilityChanged:(NSNotification *)note
{
NSString * connectionKind = nil;
Reachability * curReach = [note object];
NSParameterAssert([curReach isKindOfClass:[Reachability class]]);
netstatus = [curReach currentReachabilityStatus];
switch (netstatus)
{
case NotReachable:
connectionKind = @"当前没有网络链接\n请检查你的网络设置";
NSLog(@"%@",connectionKind);
isConnected =NO;
break;
case ReachableViaWiFi:
connectionKind = @"当前使用的网络类型是WIFI";
NSLog(@"%@",connectionKind);
isConnected =YES;
break;
case ReachableViaWWAN:
connectionKind = @"您现在使用的是2G/3G网络\n可能会产生流量费用";
NSLog(@"%@",connectionKind);
isConnected =YES;
break;
default:
break;
}
if (isConnected == NO)
{
[Common showAlert:connectionKind];
}
}
demo链接 http://download.csdn.net/my
标签:网络
原文地址:http://blog.csdn.net/tubiebutu/article/details/44804435