前提
由于大陆工信部出台的新规指出,应用在未经用户允许的前提下,体系不能授予其使用联网、获取定位的功能。Apple在iOS10体系中加入了关于应用使用数据的授权弹窗提示,用户在iOS体系及以上体系中第一次打开应用时,会被要求对于是否授予应用联网权限进行选择。
题目
很多开发者风俗把预加载接口放到AppDelegate的- (BOOL)applicationUIApplication *)application didFinishLaunchingWithOptionsNSDictionary *)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];
- }
复制代码 免责声明:如果侵犯了您的权益,请联系站长,我们会及时删除侵权内容,谢谢合作!更多信息从访问主页:qidao123.com:ToB企服之家,中国第一个企服评测及商务社交产业平台。 |