viewController中
-(void)openNotification
{
if (ctm == 0) {
return;
}
UILocalNotification *notification=[[UILocalNotification alloc] init];
if (notification!=nil)
{
NSDate *now=[NSDate new];
notification.fireDate=[now dateByAddingTimeInterval:ctm*600];
notification.timeZone=[NSTimeZone defaultTimeZone];
notification.soundName = UILocalNotificationDefaultSoundName;
notification.applicationIconBadgeNumber = 1;
notification.alertBody = [NSString stringWithFormat:AlertBody];
NSDictionary *dict = [NSDictionary dictionaryWithObjectsAndKeys:
[NSString stringWithFormat:@"tab2"], @"key1", nil];
[notification setUserInfo:dict];
[[UIApplication sharedApplication] scheduleLocalNotification:notification];
}
// [[UIApplication sharedApplication] presentLocalNotificationNow:notification];
//[swt setOn:YES animated:NO];
}
-(void)closeNotification
{
UILocalNotification *not;
for (UILocalNotification *local in [[UIApplication sharedApplication] scheduledLocalNotifications]) {
if ([[local.userInfo objectForKey:@"key1"] isEqualToString:@"tab2"]) {
not = local;
}
}
if (not) {
[[UIApplication sharedApplication] cancelLocalNotification:not];
}
// [swt setOn:NO animated:NO];
}
appDelegate中
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{
// Override point for customization after application launch.
application.applicationIconBadgeNumber = 0;
if (![[NSUserDefaults standardUserDefaults] objectForKey:@"2ndState"]) {
NSDictionary *dic = [[NSDictionary alloc] initWithObjectsAndKeys:@"60",@"max",@"0",@"now",@"0",@"state",nil];
[[NSUserDefaults standardUserDefaults] setObject:dic forKey:@"2ndState"];
}
return YES;
}
-(void)application:(UIApplication *)application didReceiveLocalNotification:(UILocalNotification *)notification
{
if (notification) {
[[UIApplication sharedApplication]cancelLocalNotification:notification];
}
UIAlertView * alert=[[UIAlertView alloc]initWithTitle:@"通知" message:notification.alertBody delegate:self cancelButtonTitle:@"ok" otherButtonTitles:nil];
[alert show];
}
-(void)alertView:(UIAlertView *)alertView didDismissWithButtonIndex:(NSInteger)buttonIndex
{
FourthViewController *viewController = [FourthViewController sharedInstance];
[viewController.swt setOn:NO animated:YES];
[viewController.sld setEnabled:NO];
NSMutableDictionary *dic = [[NSMutableDictionary alloc] initWithDictionary:[[NSUserDefaults standardUserDefaults] objectForKey:@"2ndState"]];
[dic setObject:[NSString stringWithFormat:@"%d",(int)[viewController.swt isOn]] forKey:@"state"];
[[NSUserDefaults standardUserDefaults] setObject:(NSDictionary*)dic forKey:@"2ndState"];
}
- (void)applicationWillResignActive:(UIApplication *)application
{
application.applicationIconBadgeNumber = 0;
// Sent when the application is about to move from active to inactive state. This can occur for certain types of temporary interruptions (such as an incoming phone call or SMS message) or when the user quits the application and it begins the transition to the background state.
// Use this method to pause ongoing tasks, disable timers, and throttle down OpenGL ES frame rates. Games should use this method to pause the game.
}