Turn on and off ios alarms

I prepared the Alarm Clock application, which is used UILocalnotificationfor alarm planning. Now after the alarm has been set, I want to make a switch so that I can turn it on and off using UISwitch. I just how can I do this? In my opinion, now that you turn off the alarm, I must save the DATE and TIME values ​​before canceling UILocalnotification, so that when I turn on the user again, the alarm will be rescheduled with the saved DATE and TIME values. Is this right, or are there other ways to do this?

+5
source share
1 answer

just create a database table that has a date, isCanceled field and a unique alarmId identifier (use whatever else you want). so when the user wants to cancel the alarm, try this,

    NSString *alarmId = @"some_id_to_cancel"; 
    UILocalNotification *notificationToCancel=nil;            
    for(UILocalNotification *aNotif in [[UIApplication sharedApplication] scheduledLocalNotifications]) {
        if([aNotif.userInfo objectForKey:@"ID"] isEqualToString:alarmId]) { 
            notificationToCancel = aNotif; 
            break; 
        } 
    } 
    [[UIApplication sharedApplication] cancelLocalNotification:notificationToCancel];

To use it better, you create your own alarm clock,

UILocalNotification *localNotif = [[UILocalNotification alloc] init]; 

 if (localNotif == nil)  
  return;

 localNotif.fireDate = itemDate; 
 localNotif.timeZone = [NSTimeZone defaultTimeZone];
 localNotif.alertAction = NSLocalizedString(@"View Details", nil); 
 localNotif.alertBody = title;
 localNotif.soundName = UILocalNotificationDefaultSoundName; 

 NSDictionary *infoDict = [NSDictionary dictionaryWithObject:stringID forKey:@"ID"]; 
 localNotif.userInfo = infoDict; 

 [[UIApplication sharedApplication] scheduleLocalNotification:localNotif]; 
 [localNotif release];
+7
source

All Articles