可以UILocalNotification
存放在NSUserDefaults
?
我试图获取该值,但它总是返回 null。
这是尝试获取值的方法
-(IBAction)doneClicked:(id)sender
{
NSUserDefaults *prefx = [NSUserDefaults standardUserDefaults];
UILocalNotification *oldNotifObj = [prefx objectForKey:@"NotificationObject"];
NSLog(@"oldNotifObj = %@",oldNotifObj);
NSLog(@"enable notification");
if (oldNotifObj == nil)
{
[self addNotification];
NSLog(@"add a new one");
}
else
{
//if notification exist, remove old one, and add new one.
[self removeNotification:oldNotifObj];
[prefx setObject:nil forKey:@"NotificationObject"];
[self addNotification];
NSLog(@"remove old notification and add a new one");
}
}
这是addNotification
方法
-(void)addNotification
{
//set the notification
UILocalNotification *localNotif = [[UILocalNotification alloc]init];
localNotif.fireDate = self.timePicker.date;
localNotif.repeatInterval = NSDayCalendarUnit;
localNotif.alertBody = @"Hello world!";
localNotif.soundName = UILocalNotificationDefaultSoundName;
[[UIApplication sharedApplication]scheduleLocalNotification:localNotif];
NSLog(@"localNotif = %@",localNotif);
//saving notification to prefs
NSUserDefaults *prefs = [NSUserDefaults standardUserDefaults];
[prefs setObject:localNotif forKey:@"NotificationObject"];
[prefs synchronize];
}
oldNotifObj
总是返回null 。