設定したUILocalNotificationを中止・キャンセルする方法

設定したUILocalNotificationを中止・キャンセルする方法

UILocalNotificationを使うことで指定時間になったら
アラートを出すことができることがわかりました。

そこで次に疑問に思うことはUILocalNotificationで
設定したものをどうやってキャンセルするのかってことです。

6時にアラームをセットしたけど
やっぱり6時30分にしようと思ったときのための
処理の仕方です。

これを実現するためにはUILocalNotificationを設定するときに
「userInfo」を使って特定できるようにキーを設定します。

notification.userInfo=[NSDictionary  dictionaryWithObject:@"item"
                                       forKey:@"key_id"];

構文としてはこんな感じで「NSDictionary」を使って指定します。
「forKey」が机の引き出しみたいなもので
「dictionaryWithObject」が引き出しの中身みたいなものです。

UILocalNotificationの設定する処理

UILocalNotification *notification = [[UILocalNotification alloc] init];
NSURL *urlForSomeObject=[[(NSManagedObject *)someObject objectID]
                                                   URIRepresentation];
notification.fireDate  = aDate;
notification.timeZone  = [NSTimeZone systemTimeZone];
notification.soundName = UILocalNotificationDefaultSoundName;
notification.applicationIconBadgeNumber = 1;
notification.userInfo=[NSDictionary  dictionaryWithObject:@"item"
                                       forKey:@"key_id"];

UILocalNotificationをキャンセルする処理

int deleteld = 1;
int keyId = 0;
for (UILocalNotification *notify in [[UIApplication sharedApplication]
                                                             scheduledLocalNotifications]) {
keyId = [[notify.userInfo objectForKey:@"key_id"] intValue];
	if( deleteId == keyId ){
		[[UIApplication sharedApplication] cancelLocalNotification:notify];
	 }
}

参考サイト


よくわかるiPhoneアプリ開発の教科書【iOS 5&Xcode 4.2対応版】

タイトルとURLをコピーしました