iT邦幫忙

2025 iThome 鐵人賽

DAY 21
0
Mobile Development

我將點燃Swiftの大海系列 第 21

Day21. Swift一定要會のios鬧鐘復刻實作篇 (2)

  • 分享至 

  • xImage
  •  

RealmSwift

在大家都依照之前的要求建好需要的 ViewControllertableViewCell
我相信大家應該沒這麼快忘記設定 Realm 資料庫吧!
今天帶大家來實作鬧鐘要用的 model 吧!( 昨天說的 MVC 架構

資料庫設定

同樣的我們要先設好 import 以及資料庫的 class

import Foundation
import RealmSwift

class AlarmData: Object {
    ...
}

參數設定

依照昨天我們專案的需求我們需要設定的有

  • 鬧鐘要響的時間
  • 創建鬧鐘的時間
  • 設定的鬧鐘名稱
  • 設定重複天數的 bool (用 true 和 false 來標記哪些天要重複)
  • 選擇鬧鐘響鈴聲的 ( 可用可不用 )
  • 控制鬧鐘的開關
  • 控制鬧鐘的稍後提醒

這些參數,所以我們一一填入

    //鬧鐘要響的時間
    @Persisted var alarmTime: String = "" 
    //創建鬧鐘的時間
    @Persisted var creatTime: String = "" 
    //設定的鬧鐘名稱
    @Persisted var name: String = "" 
    //設定重複天數的 bool
    @Persisted var repeatDays: List<Bool> = List<Bool>() 
    //選擇鬧鐘響鈴聲的
    @Persisted var sound: String = "" 
    //控制鬧鐘的開關
    @Persisted var isEnabled: Bool = true 
    //控制鬧鐘的稍後提醒
    @Persisted var isSnoozeOn: Bool = true 

在設定好後我們做一個簡易初始化器讓其他頁面也可以使用這些參數

    convenience init(alarmTime: String, 
                     creatTime: String, 
                     name: String, 
                     repeatDays: [Bool] = Array(repeating: false, 
                                                count: 7), 
                     sound: String, 
                     isSnoozeOn: Bool = true) {
        
            self.init()
            self.alarmTime = alarmTime
            self.creatTime = creatTime
            self.name = name
            self.sound = sound
            self.repeatDays.append(objectsIn: repeatDays)
            self.isSnoozeOn = isSnoozeOn
        
        }

設定通知

AppDelegate

接下來我們來設定要讓使用者可以跳通知選項
要先去 AppDelegate 請求權限
先 import UserNotifications 的套件

import UserNotifications

接著要在 class 也設定 UNUserNotificationCenterDelegate

class AppDelegate: UIResponder, UIApplicationDelegate, UNUserNotificationCenterDelegate{
    ...
}

最後在 class 裡新增一個 @objc

    @objc func userNotificationCenter(_ center: UNUserNotificationCenter, 
    willPresent notification: UNNotification, 
    withCompletionHandler completionHandler: @escaping (UNNotificationPresentationOptions) -> Void) {
        NotificationCenter.default.post(name: NSNotification.Name("AlarmDidFire"), 
                                        object: notification)
        completionHandler([.alert, 
                           .sound, 
                           .badge])
    }

MainView

接著來到 MainView 裡,我們要來設定一個 func 來做請求通知權限
一樣先來 import UserNotifications

import UserNotifications

接著設定 func 請求權限

    func requestNotificationPermission() {
        UNUserNotificationCenter.current().requestAuthorization(options: [.alert, .sound, .badge]) { granted, error in
            if granted {
                print("Notification permission granted")
            } else {
                print("Notification permission denied")
            }
        }
    }

最後在包進 viewDidLoad 中就完成啦!


上一篇
Day20. Swift一定要會のios鬧鐘復刻實作篇 (1)
系列文
我將點燃Swiftの大海21
圖片
  熱門推薦
圖片
{{ item.channelVendor }} | {{ item.webinarstarted }} |
{{ formatDate(item.duration) }}
直播中

尚未有邦友留言

立即登入留言