Upgrade to Pro — share decks privately, control downloads, hide ads and more …

AlarmKitで明後日起きれるアラームアプリを作る

 AlarmKitで明後日起きれるアラームアプリを作る

Avatar for Trickart

Trickart

May 15, 2026

More Decks by Trickart

Other Decks in Programming

Transcript

  1. 明後日を指定する - .fixed モードによる日時指定 AlarmKitの Alarm.Schedule には3つのスケジュール方式がある .relative(repeats: .never) …

    24時間以内・単発 .relative(repeats: .weekly([.monday])) … 毎週月曜 .fixed(Date) … 特定の日の特定時刻 ← あさってアラームはこれ // 明後日の日付 + 7:00 を組み立てる var components = Calendar.current.dateComponents( [.year, .month, .day], from: dayAfterTomorrow ) components.hour = 7 components.minute = 0 // Alarm.Schedule生成 let schedule: Alarm.Schedule = .fixed(Calendar.current.date(from: components)!) 6
  2. Alarm Configurationにscheduleを渡して スケジュールする let configuration = AlarmManager.AlarmConfiguration( countdownDuration: .init(preAlert: nil,

    postAlert: 5 * 60), // Snoozeの長さ schedule: schedule, // ← さっきの .fixed attributes: AlarmAttributes(presentation: .init(alert: alertContent), tintColor: .orange), stopIntent: StopAlarmIntent(alarmID: id.uuidString), // LiveActivityIntent secondaryIntent: SnoozeAlarmIntent(alarmID: id.uuidString) // LiveActivityIntent ) try await AlarmManager.shared.schedule(id: id, configuration: configuration) attributes タイトルや色を設定できる 7
  3. これでよいかと思いきや…落とし穴 タイムゾーン問題 .fixed(Date) は UTC絶対時刻 として保存される 東京(UTC+9)で「4/12 7:00」設定 → UTC

    4/11 22:00 ニューヨーク(UTC-4)に移動すると… → 現地 4/11 18:00 に鳴る APIの仕様としては自然だが、自分のユースケースとしてはちょっと困る → タイムゾーン変更を検知して 再スケジュール が必要 8
  4. 3層補正アーキテクチャ アプリ起動時・アプリが閉じられているとき・アプリ起動中の3つでカバーする 対策 手段 メリット・デメリット 1 起動時に lastKnownTimeZone と比較 権限不要・確実

    開かないと直らない 2 BGAppRefreshTask で定期チェック 起動しなくても動く 実行保証なし 3 NSSystemTimeZoneDidChange を購読 即時反映 起動中限定 9