前提
由于大陆工信部出台的新规指出,应用在未经用户允许的前提下,系统不能授予其使用联网、获取定位的功能。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];
}
发表评论