본문 바로가기

Programming/iPhone

NSUserDefaults 에 UILocalNotification 저장하기


NSUserDefaults 에 UILocalNotification을 저장하는 목적은 fireDate가 초과되지 않은 LocalNotification을 차후에 취소하기 위해서인데요, 기본적으로 NSUserDefault 에 UILocalNotification 객체를 그대로 저장하려고 하면 오류가 발생합니다.
이를 해결하기 위해서는 NSUserDefaults 에 저장하기 전에 약간의 작업을 더 해주어야 합니다.
(아래의 예제에서는 UILocalNotification 형의 notify 변수가 이미 설정되어있다고 가정합니다.)



1. NSUserDefaults 에 UILocalNotification 저장
NSData *data = [NSKeyedArchiver archivedDataWithRootObject:notify];
[[NSUserDefaults standardUserDefaults] setObject:data forKey:<key value>];



2. NSUserDefaults 에 저장된 UILocalNotification 읽기
NSData *data = [[NSUserDefaults standardUserDefaults] objectForKey:<key value>];
UILocalNotification *notify = [NSKeyedUnarchiver unarchiveObjectWithData:data];

if(notify){ // Local Notification Cancel
        [[UIApplication sharedApplication] cancelLocalNotification:notify];
}