1、把第三方文件 Reachability.h 和 Reachability.m添加到工程中,更改arc编译模式。
NSString *connectKind = nil;
Reachability *r = [Reachability reachabilityWithHostName:@"www.apple.com"];
NetworkStatus netStatus = [r currentReachabilityStatus];
switch (netStatus) {
case NotReachable:
connectKind = @"当前没有网络链接\n请检查你的网络设置";
_connect =NO;
break;
case ReachableViaWiFi:
connectKind = @"当前使用的网络类型是WIFI";
_connect =YES;
break;
case ReachableViaWWAN:
connectKind = @"您现在使用的是2G/3G网络\n可能会产生流量费用";
_connect =YES;
break;
default:
break;
}
UIAlertView * v = [[UIAlertView alloc] initWithTitle:@"温馨提示" message:connectKind delegate:nil cancelButtonTitle:@"我知道了" otherButtonTitles:nil, nil];
2、在AppDelegate 登陆完成方法中调入[self
currntNetworkTyp
]
3、添加方法,调用此方法,得到当前网络是否联网,以及联网类型。
- (NSString *)
- (NSString *)
currntNetworkTyp
{
Reachability *r = [Reachability reachabilityWithHostName:@"www.apple.com"];
NetworkStatus netStatus = [r currentReachabilityStatus];
switch (netStatus) {
case NotReachable:
connectKind = @"当前没有网络链接\n请检查你的网络设置";
_connect =NO;
break;
case ReachableViaWiFi:
connectKind = @"当前使用的网络类型是WIFI";
_connect =YES;
break;
case ReachableViaWWAN:
connectKind = @"您现在使用的是2G/3G网络\n可能会产生流量费用";
_connect =YES;
break;
default:
break;
}
UIAlertView * v = [[UIAlertView alloc] initWithTitle:@"温馨提示" message:connectKind delegate:nil cancelButtonTitle:@"我知道了" otherButtonTitles:nil, nil];
[v show];
}