- (void)applicationDidEnterBackground:(UIApplication *)application {
if (yesLogin) {
dispatch_async(dispatch_get_main_queue(), ^{
[self add];
yesLogin = YES;
});
}
NSLog(@"self.EffectScenView.subviews%@",self.EffectScenView.subviews);
self.date = [NSDate date];
NSLog(@"self.date%@",self.date);
NSLog(@"进入后台---applicationDidEnterBackground----"); //进入后台
}
- (void)applicationDidBecomeActive:(UIApplication *)application {
dispatch_async(dispatch_get_main_queue(), ^{
[self remoScenView];
NSLog(@"yesLogin%hhd",yesLogin);
NSDate *changeDate = [NSDate date];
if ([self.date isThisTodayHour] && yesLogin) {
NSLog(@"++++++++++++++++++++++++++++++++");
if ( [changeDate deltaFrom:self.date].minute >= 15) {
NSLog(@"11111111111111+");
dispatch_async(dispatch_get_main_queue(), ^{
LoginViewController *logn = [LoginViewController new];
[UIApplication sharedApplication].keyWindow.rootViewController = logn;
});
}
}
if ([self.date isThisTodayPreHour] && yesLogin) {
NSCalendar *cal = [NSCalendar currentCalendar];
unsigned int unitFlags = kCFCalendarUnitYear |kCFCalendarUnitMonth | NSCalendarUnitDay |NSCalendarUnitHour | NSCalendarUnitMinute;
NSDateComponents *dd = [cal components:unitFlags fromDate:self.date];
NSInteger minute = [dd minute];
if ([changeDate deltaFrom:self.date].minute +(60 -minute) >= 15) {
dispatch_async(dispatch_get_main_queue(), ^{
LoginViewController *logn = [LoginViewController new];
[UIApplication sharedApplication].keyWindow.rootViewController = logn;
});
}
}
NSLog(@"进入前台---applicationDidBecomeActive----");//进入前台
});
}
//添加window 由于我的项目中还有其他的弹出window,为了避免冲突所以这里+2
- (void)add {
self.EffectScenView.windowLevel = UIWindowLevelAlert+2;
[self.EffectScenView makeKeyAndVisible];
}
//移除window
-(void)remoScenView{
dispatch_async(dispatch_get_main_queue(), ^{
self.EffectScenView.hidden=YES;
self.EffectScenView.rootViewController = nil;
[self.EffectScenView resignKeyWindow];
[self.EffectScenView removeFromSuperview];
});
}
//判断是否是前一个小时
- (BOOL)isThisTodayPreHour
{
//日期格式化类
NSDateFormatter *formatter = [[NSDateFormatter alloc] init];
formatter.dateFormat = @"yyyy-MM-dd";
NSDate *nowDate = [formatter dateFromString:[formatter stringFromDate:[NSDate date]]];
NSDate *selfDate = [formatter dateFromString:[formatter stringFromDate:self]]; //self 表示的是调用当前方法的对象
NSCalendar *calendar = [NSCalendar currentCalendar];
NSDateComponents *compt = [calendar components:NSCalendarUnitYear | NSCalendarUnitMonth | NSCalendarUnitDay | NSCalendarUnitHour fromDate:selfDate toDate:nowDate options:0];
//时间差是一天
return compt.year == 0 && compt.month == 0 && compt.day == 0 && compt.hour == 1;
}
//判断是否是当前这个小时
- (BOOL)isThisTodayHour
{
NSCalendar *calendar = [NSCalendar currentCalendar];
NSCalendarUnit unite = NSCalendarUnitYear | NSCalendarUnitMonth | NSCalendarUnitDay | NSCalendarUnitHour ;
NSDateComponents *nowCmpts = [calendar components:unite fromDate:[NSDate date]];
NSDateComponents *selfCmpt = [calendar components:unite fromDate:self];
return nowCmpts.year == selfCmpt.year
&& nowCmpts.month == selfCmpt.month
&& nowCmpts.day == selfCmpt.day && nowCmpts.hour == selfCmpt.hour;
}