Can UILocalNotification be stored in NSUserDefaults?
I tried to get the value but it always returns null.
Here’s the method that tries to get the value
-(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");
}
}
and here’s the addNotification method
-(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];
}
the oldNotifObj always return null.
Try to convert the values manually in to an NSDictionary and try adding that to userdefaults. That solves this.
I mean you can convert the items like
localNotif.fireDatelocalNotif.repeatIntervallocalNotif.alertBodylocalNotif.soundNameetc into string objects and set them in to an NSDictionary. So you can save them into the userdefaults.`