//获取到当前所在的视图
- (UIViewController *)presentingVC:(UIApplication *)application{
UIWindow * window = application.keyWindow;
if (window.windowLevel != UIWindowLevelNormal){
NSArray *windows = [[UIApplication sharedApplication] windows];
for(UIWindow * tmpWin in windows){
if (tmpWin.windowLevel == UIWindowLevelNormal){
window = tmpWin;
break;
}
}
}
UIViewController *result = window.rootViewController;
while (result.presentedViewController) {
result = result.presentedViewController;
}
if ([result isKindOfClass:[MYTabBarViewController class]]) {
result = [(MYTabBarViewController *)result selectedViewController];
}
if ([result isKindOfClass:[UINavigationController class]]) {
result = [(UINavigationController *)result topViewController];
}
return result;
}
#p --当应用程序退出后台进行的操作
- (void)applicationDidEnterBackground:(UIApplication *)application {
//进入后台执行的操作
UIViewController *viewC = [self presentingVC:application];
NSLog(@"viewC==%@",[viewC class]);
if ([viewC class]== [LoginViewController class]) {
[[NSUserDefaults standardUserDefaults] setObject:@"1" forKey:@"root"];
}else{
[[NSUserDefaults standardUserDefaults] setObject:@"0" forKey:@"root"];
//[self goBackForViewController];
}
}
#p --应用程序进入后台再进入前台进行的操作
- (void)applicationWillEnterForeground:(UIApplication *)application {
//进入前台执行的操作---原生页面
if ([[[NSUserDefaults standardUserDefaults] objectForKey:@"root"] isEqualToString:@"0"]) {
//进入原生页面的不作处理
[self goBackForViewController];
}
//如果停留在H5页面,重新加载,防止session过期问题
else{
[self showViewControllers];
}
}
Documents:在ios应用开发中使用应用周期进行相关的操作,可以很好处理应用生命周期之间的交互问题