LoginSignup
63
59

More than 5 years have passed since last update.

UILocalNotification 毎週指定時間に通知されるローカル通知の設定

Last updated at Posted at 2013-03-01

UILocalNotification(ローカル通知)は通知したいNSDate(日付)を設定すれば、その時間にアプリの通知を発生させることが出来ます。
毎週繰り返す設定もあるので、指定したNSDateを毎週通知することも出来ます。

バナー

SS1

ダイアログ

SS2

サンプルコード

指定の時間と曜日を毎週通知するローカル通知のサンプルです。
以前投稿した指定日以降の1週間を曜日指定で取得するメソッドを使用しています。

ViewController.m
- (void)viewDidLoad
{
    // 月 水 金 のNSDateを取得 (時間はself.datePicker.dateの時間を使用します)
    NSArray *oneweekDate = [self.datePicker.date oneWeekDateWithEnableWeekdayType:YSWeekdayTypeSunday | YSWeekdayTypeTuesday | YSWeekdayTypeFriday];
    // 取得した日付を順次ローカル通知に登録
    for (NSDate *date in oneweekDate) {
        [self configureNotificationWithDate:date];
    }
}

- (void)configureNotificationWithDate:(NSDate*)date
{
    UILocalNotification *notification = [[UILocalNotification alloc] init];
    // 通知する日付
    [notification setFireDate:date];
    // 使用するカレンダー
    notification.repeatCalendar = [[NSCalendar alloc] initWithCalendarIdentifier:NSGregorianCalendar];
    // 毎週繰り返す
    notification.repeatInterval = NSWeekCalendarUnit;
    // タイムゾーン
    [notification setTimeZone:[NSTimeZone localTimeZone]];
    // 通知する本文
    [notification setAlertBody:@"通知の本文です"];
    // 通知音(デフォルトを指定)
    [notification setSoundName:UILocalNotificationDefaultSoundName];
    // アラートタイプ(ダイアログ)の通知の場合に使用する決定ボタンの文字列
    [notification setAlertAction:@"アプリを起動"];
    // ローカル通知の登録
    [[UIApplication sharedApplication] scheduleLocalNotification:notification];
}

結果

self.datePicker.date が 3/2 8:00 だとしたら、毎週月、水、金の 8:00 にローカル通知がされます。

63
59
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
63
59