在swift中将int从字符串时间4:01递减到3:51

uxhixvfz  于 2022-12-17  发布在  Swift
关注(0)|答案(2)|浏览(138)

嗨,我想从字符串时间本地通知,我想在给定时间前10分钟通知启动。我有麻烦如何使时间从4:01到3:51,请帮助。这是我的代码

let a = "4:01"

func startNoftification(prayer: String) {
    let time = prayer.split(separator: ":").map { (x) -> Int in
    return Int(String(x))!
}
   let content = UNMutableNotificationContent()
   content.title = "Adzan"
   content.body = "it's time to sholat dzuhur"

   let gregorian = Calendar(identifier: .gregorian)
   var component = gregorian.dateComponents([.year, .month, .day, .hour, .minute, .second], from: Date())
   component.hour = time[0]
   component.minute = time[1] - 10

   guard let date = gregorian.date(from: component) else { return }
   let triggerDaily = Calendar.current.dateComponents([.hour, .minute, .second], from: date)
   let trigger = UNCalendarNotificationTrigger(dateMatching: triggerDaily, repeats: true)
   let request = UNNotificationRequest(identifier: "Hijrah", content: content, trigger: trigger)

   UNUserNotificationCenter.current().add(request) { (error) in
    if let err = error {
        print("Notif error:", err)
        return
    }
  }
}
rta7y2nd

rta7y2nd1#

这就是你想要的吗?

let time = "4:01"

let dateFormatter = DateFormatter()
dateFormatter.dateFormat = "hh:mm"

guard let date = dateFormatter.date(from: time) else {
    return 
} 

let targetTime = Date(timeInterval: -(10 * 60), since: date) // Change time interval to required value in seconds
let targetTimeString = dateFormatter.string(from: targetTime)
print(targetTimeString) // Prints 3:51

或者如果你的倒计时时间有很多时间成分,使用DateComponents

var dateComponents = DateComponents()
dateComponents.minute = -10
// Other components

if let targetTime = Calendar.current.date(byAdding: dateComponents, to: date)
    print(dateFormatter.string(from: targetTime))
}
w1jd8yoj

w1jd8yoj2#

在这里,我有解决方案合并rakesha Shastri的答案,希望它能帮助别人.
首先在appDelegate和控制器中导入UserNotification

import UserNotification

并添加didFinishWithLaunchingWithOptions的代码

func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplication.LaunchOptionsKey: Any]?) -> Bool {

    UNUserNotificationCenter.current().requestAuthorization(options: [.alert, .badge]) { (granted, error) in
        if let err = error {
            print("Notifications permission denied because:", err)
            return
        }

        if granted {
            print("Notifications permission granted.")
        }
    }
}

您可以创建一个函数,并添加来自API或任意字符串的字符串时间参数,因为我没有使用函数,所以我在viewDidLoad中传递了我的解决方案

let adzan = "4:01"

let content = UNMutableNotificationContent()
    content.title = "Adzan"
    content.body = "It's time for sholat"
    content.sound = UNNotificationSound.default

    // Format the date first from string time
    let dateFormatter = DateFormatter()
    dateFormatter.dateFormat = "HH:mm"

    //addingTimeInterval is where you want to set notification before or after deciding time in this case I set it 10 minute before deciding time
    guard let date = dateFormatter.date(from: adzan)?.addingTimeInterval(-600) else { return }

    // and convert our date into dateComponents
    let triggerDate = Calendar.current.dateComponents([.hour, .minute, .second], from: date)

    let trigger = UNCalendarNotificationTrigger(dateMatching: triggerDate, repeats: true)

    let request = UNNotificationRequest(identifier: UUID().uuidString, content: content, trigger: trigger)

    UNUserNotificationCenter.current().add(request)

相关问题