0
通知通知を作成しています。特定の時刻にローカル通知が発生しています。通知のオン/オフを切り替える機能も実装しています。うまくいきません。私は以下のコードを使用しています。このコードに何か間違っています。助けてください。事前に感謝します。ローカル通知がiPhoneでキャンセルされない
- (無効)SWIT {
if (flag==1) {
//if (toggleSwitch.on) {
toggleSwitch.on=YES;
flag=1;
NSCalendar *calendar = [NSCalendar autoupdatingCurrentCalendar];
// Get the current date
NSDate *pickerDate = [self.datePicker date];
// Break the date up into components
NSDateComponents *dateComponents = [calendar components:(NSYearCalendarUnit | NSMonthCalendarUnit | NSDayCalendarUnit)
fromDate:pickerDate];
NSDateComponents *timeComponents = [calendar components:(NSHourCalendarUnit | NSMinuteCalendarUnit | NSSecondCalendarUnit)
fromDate:pickerDate];
// Set up the fire time
NSDateComponents *dateComps = [[NSDateComponents alloc] init];
[dateComps setDay:[dateComponents day]];
[dateComps setMonth:[dateComponents month]];
[dateComps setYear:[dateComponents year]];
[dateComps setHour:[timeComponents hour]];
// Notification will fire in one minute
[dateComps setMinute:[timeComponents minute]];
[dateComps setSecond:[timeComponents second]];
NSDate *itemDate = [calendar dateFromComponents:dateComps];
[dateComps release];
if (localNotif == nil)
return;
localNotif.fireDate = itemDate;
NSLog(@"%@",itemDate);
localNotif.timeZone = [NSTimeZone defaultTimeZone];
// Notification details
localNotif.alertBody = @"Tip of the day";
// Set the action button
localNotif.alertAction = @"View";
localNotif.soundName = UILocalNotificationDefaultSoundName;
localNotif.applicationIconBadgeNumber = 1;
// Specify custom data for the notification
NSDictionary *infoDict = [NSDictionary dictionaryWithObject:@"someValue" forKey:@"someKey"];
localNotif.userInfo = infoDict;
// Schedule the notification
[[UIApplication sharedApplication] scheduleLocalNotification:localNotif];
NSLog(@"it is working");
}
else {
toggleSwitch.on=NO;
[[UIApplication sharedApplication] cancelLocalNotification:localNotif];
NSLog(@"it is not working");
}
}