// AppDelegate.m内方法 程序进入后台时触发
- (void)applicationDidEnterBackground:(UIApplication *)application {
// Use this method to release shared resources, save user data, invalidate timers, and store enough application state information to restore your application to its current state incase it is terminated later.
// If your application supports background execution, this method is called instead ofapplicationWillTerminate: when the user quits.
NSInteger times = [[[NSUserDefaults standardUserDefaults] objectForKey:@"GoBackground"] integerValue];
times++;
[[NSUserDefaults standardUserDefaults]setInteger:times forKey:@"GoBackground"];
[[NSUserDefaults standardUserDefaults] synchronize];
}
在用户唤起app的时候,我们可以提示用户开启推送权限
// AppDelegate.m内方法 程序唤起时触发
- (void)applicationWillEnterForeground:(UIApplication *)application {
// Called as part of the transition from the background to the active state; here you can undo many of the changes made on entering the background.NSInteger times = [[[NSUserDefaults standardUserDefaults] objectForKey:@"GoBackground"] integerValue];
if (times >= 3) {
// ios9之后 将会被UIAlertController替代
UIAlertView * alert = [[UIAlertView alloc]initWithTitle:@"温馨提示" message:@"打开推送权限" delegate:self cancelButtonTitle:@"取消" otherButtonTitles:@"允许", nil];
[alert show];
}
}
- (void)alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex{
if (buttonIndex != 0) {
// 用户点击允许 ios8之后,可以直接调转手机设置界面if([UIDevice currentDevice].systemVersion.integerValue >= 8.0){
[self goToSetting];
}
}
}
// 跳转到设置界面
- (void)goToSetting{
[[UIApplication sharedApplication] openURL:[NSURL URLWithString:UIApplicationOpenSettingsURLString]];
}