开发者

NSUserDefaults + Local Notifications

开发者 https://www.devze.com 2023-03-24 03:46 出处:网络
I use in my app the local notifications for reminders. I need to set any alarm with a name so you have an alarm only for each activity. I would use NSUserDefaults to have key-value. Now I use this sys

I use in my app the local notifications for reminders. I need to set any alarm with a name so you have an alarm only for each activity. I would use NSUserDefaults to have key-value. Now I use this system that does not al开发者_运维知识库low me to do this.

-(void)scheduleAlarm:(id)sender {

NSCalendar *calendar = [NSCalendar autoupdatingCurrentCalendar];

// Get the current date
NSDate *pickerDate = [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];

UILocalNotification *localNotif = [[UILocalNotification alloc] init];
if (localNotif == nil)
    return;
localNotif.fireDate = itemDate;
localNotif.timeZone = [NSTimeZone defaultTimeZone];

// Notification details
localNotif.alertBody = [mainTitle text];
// Set the action button
localNotif.alertAction = @"View";

localNotif.soundName = UILocalNotificationDefaultSoundName;

// Specify custom data for the notification
NSDictionary *infoDict = [NSDictionary dictionaryWithObject:@"someValue" forKey:@"someKey"];
localNotif.userInfo = infoDict;

// Schedule the notification
[[UIApplication sharedApplication] scheduleLocalNotification:localNotif];
[localNotif release];





}

thanks a lot!


NSUserDefaults has this instance method:

- (NSDictionary *)dictionaryForKey:(NSString *)defaultName

Why not use it to return the dictionary that you need for the userInfo property of your Local Notification?

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号