前提
由于大陆工信部出台的新规指出,应用在未经用户允许的前提下,系统不能授予其使用联网、获取定位的功能。Apple在iOS10系统中加入了关于应用使用数据的授权弹窗提示,用户在iOS系统及以上系统中第一次打开应用时,会被要求对于是否授予应用联网权限进行选择。
问题
很多开发者习惯把预加载接口放到AppDelegate的- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions方法里面,但是现在第一次安装,默认是没有联网能力的,按之前的写法会导致配置接口请求失败,首页数据为空,第一次获取DeviceToken失败,添加推送通知失败,第三方初始化失败等等一系列问题。
解决方法
使用CTCellularData加AFNetworkReachabilityManager
CTCellularData类用于获取网络权限状态以及监听状态改变回调
AFNetworkReachabilityManager用于实时监测当前的网络状态
上代码
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
//1.获取网络权限 根据权限进行人机交互
if (__IPHONE_10_0) {
[self networkStatus:application didFinishLaunchingWithOptions:launchOptions];
}else {
//2.2已经开启网络权限 监听网络状态
[self addReachabilityManager:application didFinishLaunchingWithOptions:launchOptions];
}
self.window = [[UIWindow alloc] initWithFrame:CGRectMake(0,0, KScreenWidth, KScreenHeight)];
self.window.backgroundColor = [UIColor whiteColor];
MainViewController *vc = [[MainViewController alloc] init];
self.window.rootViewController = vc;
[self.window makeKeyAndVisible];
return YES;
}
/*
获取网络权限状态
*/
- (void)networkStatus:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
//2.根据权限执行相应的交互
CTCellularData *cellularData = [[CTCellularData alloc] init];
/*
此函数会在网络权限改变时再次调用
*/
cellularData.cellularDataRestrictionDidUpdateNotifier = ^(CTCellularDataRestrictedState state) {
switch (state) {
case kCTCellularDataRestricted:
NSLog(@"Restricted");
//2.1权限关闭的情况下 再次请求网络数据会弹出设置网络提示
break;
case kCTCellularDataNotRestricted:
NSLog(@"NotRestricted");
//2.2已经开启网络权限 监听网络状态
[self addReachabilityManager:application didFinishLaunchingWithOptions:launchOptions];
break;
case kCTCellularDataRestrictedStateUnknown:
NSLog(@"Unknown");
break;
default:
break;
}
};
}
/**
实时检查当前网络状态
*/
- (void)addReachabilityManager:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
AFNetworkReachabilityManager *afNetworkReachabilityManager = [AFNetworkReachabilityManager sharedManager];
[afNetworkReachabilityManager setReachabilityStatusChangeBlock:^(AFNetworkReachabilityStatus status) {
switch (status) {
case AFNetworkReachabilityStatusNotReachable:{
NSLog(@"网络不通:%@",@(status) );
break;
}
case AFNetworkReachabilityStatusReachableViaWiFi:{
NSLog(@"网络通过WIFI连接:%@",@(status));
[self getInfo_application:application didFinishLaunchingWithOptions:launchOptions];
break;
}
case AFNetworkReachabilityStatusReachableViaWWAN:{
NSLog(@"网络通过无线连接:%@",@(status) );
[self getInfo_application:application didFinishLaunchingWithOptions:launchOptions];
break;
}
default:
break;
}
}];
[afNetworkReachabilityManager startMonitoring]; //开启网络监视器;
}
//把以前写在- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions里面的一些初始化操作放在该方法
- (void)getInfo_application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
[self.updateBusiness checkUpdateWithBothApi];//检查app更新
//发送通知给APP首屏页面,让其有网络时重新请求
[[NSNotificationCenter defaultCenter] postNotificationName:AFNetworkingReachabilityDidChangeNotification object:nil];
}