当用户第一次打开应用的时候,如果用户还没来得及点击同意连接网络,那么网络请求是失败的,所以,为了让APP的体验更好,我们需要监听网络,如果网络发生变化,需要重新请求数据。当然,用户没联网的状况也可以得到解决!
首先在AppDelegate设置监听网络
- AppDelegate.h
@interface AppDelegate : UIResponder <UIApplicationDelegate>
/*
当前的网络状态
*/
@property(nonatomic,assign)int netWorkStatesCode;
@end
- AppDelegate.m
- (BOOL)application:(UIApplication *)application willFinishLaunchingWithOptions:(nullable NSDictionary *)launchOptions{
//网络监控
[self netWorkChangeEvent];
return YES;
}
#pragma mark - 检测网络状态变化
-(void)netWorkChangeEvent
{
[[AFNetworkActivityIndicatorManager sharedManager] setEnabled:YES];
NSURL *url = [NSURL URLWithString:@"https://www.baidu.com"];
AFHTTPSessionManager *manager = [[AFHTTPSessionManager alloc] initWithBaseURL:url];
self.netWorkStatesCode =AFNetworkReachabilityStatusUnknown;
[manager.reachabilityManager setReachabilityStatusChangeBlock:^(AFNetworkReachabilityStatus status) {
self.netWorkStatesCode = status;
switch (status) {
case AFNetworkReachabilityStatusReachableViaWWAN:
NSLog(@"当前使用的是流量模式");
break;
case AFNetworkReachabilityStatusReachableViaWiFi:
NSLog(@"当前使用的是wifi模式");
break;
case AFNetworkReachabilityStatusNotReachable:
NSLog(@"断网了");
break;
case AFNetworkReachabilityStatusUnknown:
NSLog(@"变成了未知网络状态");
break;
default:
break;
}
[[NSNotificationCenter defaultCenter] postNotificationName:@"netWorkChangeEventNotification" object:@(status)];
}];
[manager.reachabilityManager startMonitoring];
}
#pragma mark - 释放应用
-(void)dealloc{
[[NSNotificationCenter defaultCenter]removeObserver:self name:@"netWorkChangeEventNotification" object:nil];
}
然后在监听页面监听,我的首页写在RecommendViewController中。
- RecommendViewController
- (void)viewDidLoad {
[super viewDidLoad];
self.view.backgroundColor=[UIColor whiteColor];
//定制导航栏
[self CreatNavigationBar];
//无网视图
[self CreatNoNetView];
//加载瀑布流视图
[self creatCollectionView];
//先查收看是否有旧的数据,在网络差的时候优先加载旧的数据
[self loadOldData];
//网络状态发生变化的时候的通知方法
[[NSNotificationCenter defaultCenter]addObserver:self
selector:@selector(NetWorkStatesChange:) name:@"netWorkChangeEventNotification"
object:nil];
}
/**
定制导航栏
*/
-(void)CreatNavigationBar{
UILabel *Titlelabel=[[UILabel alloc] initWithFrame:CGRectMake(0, 0, KScreenW, 44)];
Titlelabel.font=[UIFont systemFontOfSize:30 weight:3.0];
Titlelabel.text=@"逗乐视频";
self.navigationItem.leftBarButtonItem=[[UIBarButtonItem alloc] initWithCustomView:Titlelabel];
self.navigationItem.rightBarButtonItem=[[UIBarButtonItem alloc] initWithImage:[[UIImage imageNamed:@"搜素"] imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal] style:UIBarButtonItemStylePlain target:self action:@selector(clickSearch)];
}
/**
无网视图
*/
-(void)CreatNoNetView{
UIView *baseView=[[UIView alloc] initWithFrame:CGRectMake((KScreenW-106)/2, (KScreenH-150-64-49)/2, 106, 150)];
baseView.backgroundColor=[UIColor clearColor];
[self.view addSubview:baseView];
//无网占位图
UIImageView *iconImage=[[UIImageView alloc] initWithFrame:CGRectMake(0, 0, 106, 100)];
[iconImage setImage:[UIImage imageNamed:@"当前无网络"]];
[baseView addSubview:iconImage];
//联网按钮
UIButton *connectBtn=[[UIButton alloc] initWithFrame:CGRectMake(0, 115, 106, 35)];
[connectBtn setTitle:@"设置网络" forState:UIControlStateNormal];
[connectBtn setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
[connectBtn setBackgroundColor:YTHColor(253, 177, 76)];
connectBtn.layer.masksToBounds=YES;
connectBtn.layer.cornerRadius=5.0f;
[connectBtn addTarget:self action:@selector(ClickConnectBtn:) forControlEvents:UIControlEventTouchUpInside];
[baseView addSubview:connectBtn];
}
// 点击进入链接WIFI的设置界面
-(void)ClickConnectBtn:(UIButton *)sender{
NSString * urlString = @"App-Prefs:root=WIFI";
if ([[UIApplication sharedApplication] canOpenURL:[NSURL URLWithString:urlString]]) {
if ([[UIDevice currentDevice].systemVersion doubleValue] >= 10.0) {
[[UIApplication sharedApplication] openURL:[NSURL URLWithString:urlString] options:@{} completionHandler:nil];
} else {
[[UIApplication sharedApplication] openURL:[NSURL URLWithString:urlString]];
}
}else{
[SVProgressHUD showInfoWithStatus:@"请手动设置"];
}
}
最后在处理接收通知的方法里面隐藏或显示相关界面
#pragma mark - 网络状态发生变化通知方法
-(void)NetWorkStatesChange:(NSNotification *)sender{
int networkState = [[sender object] intValue];
switch (networkState) {
case -1:
//未知网络状态
break;
case 0:
//没有网络
[baseView setHidden:NO];
[self.scrollPageView setHidden:YES];
break;
case 1:
//3G或者4G,反正用的是流量
[baseView setHidden:YES];
[self.scrollPageView setHidden:NO];
[self loadData];
break;
case 2:
//WIFI网络
[baseView setHidden:YES];
[self.scrollPageView setHidden:NO];
[self loadData];
break;
default:
break;
}
}
画外音:在我的项目里面,如果一进来的时候是没有网络变化的,也即是说即便没有网络,也不一定会调用这个方法,所以,我在创建视图的时候,先判断当前的网络状况,直接显示或者隐藏相关视图。
e.g:
//获取当前网络,如果为无网,直接隐藏列表视图
AppDelegate *delegate = (AppDelegate *)[UIApplication sharedApplication].delegate;
if (delegate.netWorkStatesCode == AFNetworkReachabilityStatusNotReachable) {
[baseView setHidden:NO];
}else{
[baseView setHidden:YES];
}
网友评论