1
1

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

iOS26~のアラーム実装方法(AlarmKit)

1
Last updated at Posted at 2025-08-24

上記動画とApple Developerの公開情報から抜粋しています。

機能

iOS標準の「時計」アプリ同様に、設定時刻にフルスクリーン表示されるアラームを設定できる。ロック中やスタンドバイでも表示され、接続中のApple Watchにも表示可能。サイレントモードでも音を鳴らせる。

ロック中はシステム標準の専用画面、デバイスの使用中はDynamic Islandまたはライブアクティビティに表示される。プロジェクトにWidget Extensionを追加しないと動作しない。

「重大な通知」(Critical Alerts)の代わりとして利用することはできない。

アクセス許可

通常の通知などと同様、ユーザーのアクセス許可なしにはアラームを設定できない。
Info.plistにNSAlarmKitUsageDescriptionを追加し、アラームの利用理由を指定する。

初めてアラームをセットするときに自動で許可を求めるアラートが表示されるが、任意で表示するタイミングを指定できる。

do {
    let maneger = AlarmManager.shared
    let status = try await manager.requestAuthorization()
} catch {
}

許可されているかは以下のように確認できる。

switch AlarmManager.shared.authorizationState {
    case .notDetermined:
      // 許可を求めるアラートが一度も表示されていない
    case .authorized:
      // 許可されている
    case .denied:
      // 許可されていない
}

アラームの設定

カウントダウン型

preAlertに残り時間を秒数で指定する。postAlertにはスヌーズが選択された際に再度アラームを鳴らす時を指定する。

let countdownDuration = Alarm.CountdownDuration(preAlert: カウントダウン時間, postAlert: スヌーズの時間)

アラーム型

指定時刻にアラームを設定する。年月日時分で指定する。
端末のタイムゾーンが変更されるとそれに応じて自動的にセットされる。

let DateComponents = DateComponents(
    calendar: .current,
    year: ,
    month: ,
    day: ,
    hour: ,
    minute: )
  let alertDate = Calendar.current.date(from: DateComponents)!
  let scheduleFixed = Alarm.Schedule.fixed(alertDate)

日付指定ではなく直近の時間を指定する場合は以下のように書く

let time = Alarm.Schedule.Relative.Time(hour: , minute: )
let recurrence = Alarm.Schedule.Relative.Recurrence.never
let schedule = Alarm.Schedule.Relative(time: time, repeats: recurrence)

曜日指定で繰り返しアラームをセットすることもできる。

    let time = Alarm.Schedule.Relative.Time(hour: , minute: )
  let recurrence = Alarm.Schedule.Relative.Recurrence.weekly(曜日)
  let schedule = Alarm.Schedule.Relative(time: time, repeats: recurrence)

曜日はLocale.Weekday型の配列で指定

アラームの設定とカスタマイズ

func scheduleAlarm() async throws {
    typealias AlarmConfiguration = AlarmManager.AlarmConfiguration<AlarmData>
    
    let id = UUID()

    //タイマー型の場合
    let duration = Alarm.CountdownDuration(preAlert: カウントダウン時間, postAlert: スヌーズ時間)
    // アラーム型の場合
    let DateComponents = DateComponents(
    calendar: .current,
    year: ,
    month: ,
    day: ,
    hour: ,
    minute: )
  let alertDate = Calendar.current.date(from: DateComponents)!
  let scheduleFixed = Alarm.Schedule.fixed(alertDate)

    //停止ボタン
    let stopButton = AlarmButton(
        text: "停止ボタン", //アラート画面のボタンの色
        textColor: .white, //Dynamic Islandの文字色
        systemImageName: "stop.circle")  //Dynamic Islandのアイコン

    //テキスト設定
    let alertPresentation = AlarmPresentation.Alert(
        title: "アラームの名前", //アラームの名前
        stopButton: stopButton) //停止ボタンの指定

    //デザインの設定
    let attributes = AlarmAttributes<AlarmData>(
        presentation: AlarmPresentation(
            alert: alertPresentation), //テキスト設定
        tintColor: Color.green) //テーマカラー

    //アラームをセットする
    let alarmConfiguration = AlarmConfiguration(
        countdownDuration: duration, //タイマー型の場合
        schedule: scheduleFixed, //アラーム型の場合
        attributes: attributes)
    
    try await AlarmManager.shared.schedule(id: id, configuration: alarmConfiguration)
}

struct AlarmData: AlarmMetadata {
}

アラームのセット
AlarmManager.shared.scheduleを叩くことでアラームがセットされる。
この引数にはアラームを判別するUIDと、アラーム設定を含むAlarmConfiguration()イニシャライザを格納する必要があり、後者には時間設定に加えデザインのカスタマイズも含まれる。

アラームの時刻設定は先述の通りタイマー型、アラーム型があり、AlarmConfiguration()における引数は別である。

countdownDuration: タイマー型,
schedule: アラーム型

※ 繰り返しのAlarm.Schedule.Relativeだとscheduleに指定できなかった。何か見落としていると思うが、Alarm.Schedule.relative()で囲むことで一応解決した。

AlarmConfiguration()ではアラーム名、ボタンの文字、色を指定できる。
以下のAlarmAttributes<AlarmMetadata>を作成し、attributes引数に指定する。(省略時はデフォルト値が使用される)

デザイン設定

let attributes = AlarmAttributes<AlarmData>(
    presentation: AlarmPresentation(
    alert: alertPresentation), //下記のテキスト設定
    tintColor: Color.green) //テーマカラー

テキストの指定

 let alertPresentation = AlarmPresentation.Alert(
        title: "アラームの名前", //アラームの名前
        stopButton: stopButton) //下記の停止ボタンの指定

アラーム画面はテキストのみ表示され、左にアイコンが表示される。
Dynamic Islandでも同じように表示される。

停止ボタン

let stopButton = AlarmButton(
        text: "停止ボタン", //アラート画面のボタンの色
        textColor: .white, //Dynamic Islandの文字色
        systemImageName: "stop.circle")  //Dynamic Islandのアイコン

アラーム画面はテキストのみ表示される。(固定デザインのボタン)
Dynamic Islandの表示はアイコンが表示される。SFSymbolsを指定可能。色も変更できる。

スヌーズボタン

let repeatButton = AlarmButton(
    text: "スヌーズ",
    textColor: .white,
    systemImageName: "repeat.circle")

停止ボタンと同様にスヌーズボタンを追加できる。
ボタンの色はAlarmAttributesで指定したテーマカラーになる。
Dynamic Islandでは停止ボタンの左に表示される。

let alertPresentation = AlarmPresentation.Alert(
    title: "アラームの名前",
    stopButton: stopButton,
    secondaryButton: repeatButton, //スヌーズボタン
    secondaryButtonBehavior: .countdown) //アラームの役割を定義

停止ボタンと同様にsecondaryButtonに指定する。
secondaryButtonBehaviorでは役割を指定可能。.countdownはタイマーの繰り返し、.customでカスタムアクションも指定できる。

カスタムアクションの実行

AppIntentを定義し、ボタンをが押された時にシステムがインテントを実行するようにする。

public struct OpenInApp: LiveActivityIntent {
    public func perform() async throws -> some IntentResult { .result() }
    
    public static var title: LocalizedStringResource = "Open App"
    public static var description = IntentDescription("Opens the Sample app")
    public static var openAppWhenRun = true
    
    @Parameter(title: "alarmID")
    public var alarmID: String
    
    public init(alarmID: String) {
        self.alarmID = alarmID
    }
    
    public init() {
        self.alarmID = ""
    }
}

ライブアクティビティ

カウントダウン型の場合、ライブアクティビティを作成する必要がある。追加しないとアラートは動作しない。
アラーム型はシステムがライブアクティビティを自動で表示するが、ボタンと動作のカスタマイズも可能。

音声の設定

標準ではシステム音声が利用されるが、因位のファイルを指定することもできる。
アプリのメインバンドルか、データコンテナのLibrary/Soundsフォルダに格納されたファイルを指定できる。

let sound = AlertConfiguration.AlertSound.named("ファイル名")
let configuration = AlarmConfiguration(countdownDuration: countdownDuration,atributes: atributes,sound: sound)
1
1
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
1
1

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?