用户在使用app的过程中,会出现有wifi切换到蜂窝的时候,那个时候我们应该给用户一个提示。用户如果在看视频,突然wifi网路断开变成蜂窝,如果在继续看的话,那么用户的损失会是很大的。所以即时提醒用户当前的网路状态很有必要。
直接在appdelegate方法里面加通知,要先导一下
#import "Reachability.h"
上代码:
-(void)getNetWorkNotificationCenter
{
[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(reachabilityChanged:) name:kReachabilityChangedNotification object:nil];
reachability = [Reachability reachabilityWithHostName:@"www.baidu.com"];
[reachability startNotifier];
}
- (void)reachabilityChanged:(NSNotification *)notification
{
Reachability *curReachability = [notification object];
NSParameterAssert([curReachability isKindOfClass:[Reachability class]]);
NetworkStatus curStatus = [curReachability currentReachabilityStatus];
switch(curStatus) {
case NotReachable://无网路连接
[self.window makeToast:NONETWORK
duration:1.0
position:[NSValue valueWithCGPoint:CGPointMake(SCREEN_WIDTH/2,SCREEN_HEIGHT-80)]];
[GBObject sharedInstance].isConnection=NO;
break;
case ReachableViaWWAN://蜂窝
[self.window makeToast:WINNNETWORK
duration:1.0
position:[NSValue valueWithCGPoint:CGPointMake(SCREEN_WIDTH/2,SCREEN_HEIGHT-80)]];
[GBObject sharedInstance].isConnection=YES;
break;
case ReachableViaWiFi://wifi
[GBObject sharedInstance].isConnection=YES;
break;
}
}
备注:以上为本人浅见,如有不对之处,请各位大大们指出纠正,谢谢!!