Rahul Singh
Rahul Singh

Reputation: 1229

Switching ON and OFF the Alarm ios

I have prepared an Alarm Clock app which uses UILocalnotification for scheduling the alarm.Now after the alarm has been set, I want to make a switch so that I can turn it ON and OFF usingUISwitch.I just cant figure how can I do that?What I am thinking as of now is that when you switch OFF the alarm, I shall store the DATE and TIME value before canceling the UILocalnotification so that when the user again switch ON the alarm I reschedule it with the stored DATE and TIME values. Is it the right way to do or is there any other ways to do that?

Upvotes: 5

Views: 1975

Answers (1)

Alex Markman
Alex Markman

Reputation: 1460

just make the database table which have 'date', 'isCanceled' field and unique id 'alarmId' columns (use rest whatever 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 this better you create your alarm by,

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];

Upvotes: 7

Related Questions