打开和关闭警报ios

Rah*_*ngh 5 xcode alarm ios uilocalnotification

我准备了一个闹钟应用程序,UILocalnotification用于安排闹钟.现在闹钟设置后,我想做一个开关,这样我就可以用它来打开和关闭UISwitch.我只是想不知道我该怎么做?我现在想的是,当你关闭闹钟时,我会在取消之前存储DATE和TIME值,UILocalnotification这样当用户再次打开闹钟时我会用存储的DATE和TIME值重新安排它.这是正确的做法,还是有其他方法可以做到这一点?

Ale*_*man 7

只需使数据库表具有'date','isCanceled'字段和唯一id'alarmId'列(使用任何你想要的休息).所以当用户想要取消闹钟时试试这个,

    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];
Run Code Online (Sandbox Code Playgroud)

为了更好地使用它,你可以通过

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];
Run Code Online (Sandbox Code Playgroud)