On the latest Xcode beta 7, and iOS Beta 9 I cannot get the custom alarm sound to work no matter what I try. I have tried in the simulator and on a physical device.
The "my-sound" variable points to a local file in the project, and I’ve confirmed that Bundle.main can locate it.
What am I doing wrong?
import Foundation
import AlarmKit
import SwiftUI
struct DefaultAlarmMetadata: AlarmMetadata {
let label: String
}
struct DoesNotWork: View {
public func buildAlarmConfiguration() async throws -> AlarmManager.AlarmConfiguration<DefaultAlarmMetadata> {
let stopButton = AlarmButton(text: "Stop", textColor: .red, systemImageName: "stop.circle")
let alertContent = AlarmPresentation.Alert(
title: "Mocked Alarm",
stopButton: stopButton
)
let presentation = AlarmPresentation(alert: alertContent)
let metadata = DefaultAlarmMetadata(
label: "My Mocked Label"
)
if let soundPath = Bundle.main.path(forResource: "my-sound", ofType: "caf") {
print("Sound file found at: \(soundPath)")
} else {
print("Sound file not found in bundle")
}
return AlarmManager.AlarmConfiguration.alarm(
schedule: .fixed(Date.now.addingTimeInterval(2 * 60)),
attributes: AlarmAttributes(
presentation: presentation,
metadata: metadata,
tintColor: .red
),
sound: .named("my-sound")
)
}
func registerAlarm() async throws {
let configuration = try await self.buildAlarmConfiguration()
let _ = try await AlarmManager.shared.schedule(
id: UUID(),
configuration: configuration
)
}
var body: some View {
VStack {
Button("Register Alarm") {
Task {
try await registerAlarm()
}
}
}
}
}
Topic:
App & System Services
SubTopic:
Notifications