質問編集履歴
1
プログラムと自分なりのコメントを付け足しました
title
CHANGED
File without changes
|
body
CHANGED
@@ -2,13 +2,48 @@
|
|
2
2
|
タイマーを作っているのですが、アラーム音を鳴らすコードが書けなくて困っています。iPhoneにデフォで入ってるアラーム1種類、できれば数種類、欲を言えば、musicの音楽まで設定できればいいなと思ってます。
|
3
3
|
|
4
4
|
###発生している問題・エラーメッセージ
|
5
|
+
appdelegateの方は難しくてエラー処理してることくらいしか分かりませんでした。
|
6
|
+
scehdulerの方も変数?がかろうじてわかるくらいで何をしているかがわからないです
|
5
7
|
```
|
6
8
|
|
7
9
|
```
|
8
10
|
|
9
11
|
###該当のソースコード
|
12
|
+
scheduler.swiftの一部分
|
10
13
|
```
|
11
|
-
|
14
|
+
func setNotificationWithDate(_ date: Date, onWeekdaysForNotify weekdays:[Int], snoozeEnabled:Bool, onSnooze: Bool, soundName: String, index: Int) {
|
15
|
+
let AlarmNotification: UILocalNotification = UILocalNotification()
|
16
|
+
AlarmNotification.alertBody = "Wake Up!"
|
17
|
+
AlarmNotification.alertAction = "Open App"
|
18
|
+
AlarmNotification.category = "myAlarmCategory"
|
19
|
+
//アラームの音楽を設定
|
20
|
+
AlarmNotification.soundName = soundName + ".mp3"
|
21
|
+
//時間の設定
|
22
|
+
AlarmNotification.timeZone = TimeZone.current
|
23
|
+
let repeating: Bool = !weekdays.isEmpty
|
24
|
+
AlarmNotification.userInfo = ["snooze" : snoozeEnabled, "index": index, "soundName": soundName, "repeating" : repeating]
|
25
|
+
//repeat weekly if repeat weekdays are selected
|
26
|
+
//no repeat with snooze notification
|
27
|
+
if !weekdays.isEmpty && !onSnooze{
|
28
|
+
AlarmNotification.repeatInterval = NSCalendar.Unit.weekOfYear
|
29
|
+
}
|
30
|
+
|
31
|
+
let datesForNotification = correctDate(date, onWeekdaysForNotify:weekdays)
|
32
|
+
|
33
|
+
syncAlarmModel()
|
34
|
+
for d in datesForNotification {
|
35
|
+
if onSnooze {
|
36
|
+
alarmModel.alarms[index].date = Scheduler.correctSecondComponent(date: alarmModel.alarms[index].date)
|
37
|
+
}
|
38
|
+
else {
|
39
|
+
alarmModel.alarms[index].date = d
|
40
|
+
}
|
41
|
+
AlarmNotification.fireDate = d
|
42
|
+
UIApplication.shared.scheduleLocalNotification(AlarmNotification)
|
43
|
+
}
|
44
|
+
setupNotificationSettings()
|
45
|
+
|
46
|
+
}
|
12
47
|
```
|
13
48
|
|
14
49
|
###試したこと
|