If you edit and relaunch your app your Alarm Clock will break.
Just Delete UIApplication.shared.scheduledLocalNotifications = nil
in MainAlarmViewController.swift.
To resolve trouble with getting notifications in other VC and getting notifications when app closed make this:
` func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplicationLaunchOptionsKey: Any]?) -> Bool {
alarmScheduler.setupNotificationSettings()
window?.tintColor = UIColor.red
let notificationSettings = UIUserNotificationSettings(types: .alert, categories: nil)
UIApplication.shared.registerUserNotificationSettings(notificationSettings)
if let notification = launchOptions?[UIApplicationLaunchOptionsKey.localNotification] as? UILocalNotification {
if let userInfo = notification.userInfo {
let index = userInfo["index"] as! Int
let storyboard = UIStoryboard(name: "Main", bundle: nil)
let mainVC = storyboard.instantiateViewController(withIdentifier: "Alarm") as! MainAlarmViewController
if mainVC.alarmModel.alarms[index].repeatWeekdays.isEmpty {
mainVC.alarmModel.alarms[index].enabled = false
}
}
DispatchQueue.main.async {
let alertController = UIAlertController(title: "Alarm", message: nil, preferredStyle: .alert)
alertController.addAction(UIAlertAction(title: "OK", style: .default, handler: nil))
self.window?.rootViewController?.present(alertController, animated: true, completion: nil)
}
}
return true
}
//receive local notification when app in foreground
func application(_ application: UIApplication, didReceive notification: UILocalNotification) {
//show an alert window
let storageController = UIAlertController(title: "Alarm", message: nil, preferredStyle: .alert)
var isSnooze: Bool = false
var soundName: String = ""
var index: Int = -1
if let userInfo = notification.userInfo {
isSnooze = userInfo["snooze"] as! Bool
soundName = userInfo["soundName"] as! String
index = userInfo["index"] as! Int
}
playSound(soundName)
//schedule notification for snooze
if isSnooze {
alarmScheduler.setNotificationForSnooze(snoozeMinute: 9, soundName: soundName, index: index)
let snoozeOption = UIAlertAction(title: "Snooze", style: .default) {
(action:UIAlertAction)->Void in self.audioPlayer?.stop()
}
storageController.addAction(snoozeOption)
}
let stopOption = UIAlertAction(title: "OK", style: .default) {
(action:UIAlertAction)->Void in self.audioPlayer?.stop()
//change UI
if let mainVC = self.window?.visibleViewController as? MainAlarmViewController {
if mainVC.alarmModel.alarms[index].repeatWeekdays.isEmpty {
mainVC.alarmModel.alarms[index].enabled = false
}
let cells = mainVC.tableView.visibleCells
for cell in cells {
if cell.tag == index {
let sw = cell.accessoryView as! UISwitch
if mainVC.alarmModel.alarms[index].repeatWeekdays.isEmpty {
sw.setOn(false, animated: false)
cell.backgroundColor = UIColor.groupTableViewBackground
cell.textLabel?.alpha = 0.5
cell.detailTextLabel?.alpha = 0.5
}
}
}
} else {
let storyboard = UIStoryboard(name: "Main", bundle: nil)
let mainVC = storyboard.instantiateViewController(withIdentifier: "Alarm") as! MainAlarmViewController
if mainVC.alarmModel.alarms[index].repeatWeekdays.isEmpty {
mainVC.alarmModel.alarms[index].enabled = false
}
}
}
storageController.addAction(stopOption)
window?.visibleViewController?.navigationController?.present(storageController, animated: true, completion: nil)
}`
Make Extension.swift
`import Foundation
import UIKit
public extension UIWindow {
public var visibleViewController: UIViewController? {
return UIWindow.getVisibleViewControllerFrom(vc: self.rootViewController)
}
public static func getVisibleViewControllerFrom(vc: UIViewController?) -> UIViewController? {
if let nc = vc as? UINavigationController {
return UIWindow.getVisibleViewControllerFrom(vc: nc.visibleViewController)
} else if let tc = vc as? UITabBarController {
return UIWindow.getVisibleViewControllerFrom(vc: tc.selectedViewController)
} else {
if let pvc = vc?.presentedViewController {
return UIWindow.getVisibleViewControllerFrom(vc: pvc)
} else {
return vc
}
}
}
If you edit and relaunch your app your Alarm Clock will break. Just Delete
UIApplication.shared.scheduledLocalNotifications = nil
in MainAlarmViewController.swift.To resolve trouble with getting notifications in other VC and getting notifications when app closed make this: ` func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplicationLaunchOptionsKey: Any]?) -> Bool { alarmScheduler.setupNotificationSettings() window?.tintColor = UIColor.red
Make Extension.swift `import Foundation
import UIKit
public extension UIWindow { public var visibleViewController: UIViewController? { return UIWindow.getVisibleViewControllerFrom(vc: self.rootViewController) }
} `
That's all