ios - 本地通知 - 重复间隔(Xcode,swift2)

标签 ios xcode swift datepicker

检查我的重复间隔代码。我认为它是错误的。我只希望用户选择通知的时间及其工作,但它还有一个问题,即它在每 13 或 14 或任何一分钟后重复,我不知道为什么所以请检查下面的代码,是否有人可以每周和每月帮助我?

   //canceling notifications
func cancelLocalNotificationsWithUUID(uuid: String) {
    for item in UIApplication.sharedApplication().scheduledLocalNotifications {
        let notification = item as! UILocalNotification
        if let notificationUUID = notification.userInfo?["UUID"] as? String {
            if notificationUUID == uuid {
                UIApplication.sharedApplication().cancelLocalNotification(notification)
            }
        }
    }
}

@IBAction func NotificationButtonTapped(sender: AnyObject) {

     cancelLocalNotificationsWithUUID("NotificationID")

    var dateFormatter = NSDateFormatter()
    dateFormatter.dateFormat = "HH:mm"
    var strDate = dateFormatter.stringFromDate(datePicker.date)
    PickedDate.text = "Your Notification time you choosed is \(strDate)"
   PickedDate.numberOfLines = 0


    var notifications:UILocalNotification = UILocalNotification()
    notifications.fireDate = datePicker.date
    notifications.timeZone = NSTimeZone.defaultTimeZone()
    notifications.applicationIconBadgeNumber =    UIApplication.sharedApplication().applicationIconBadgeNumber + 1
    notifications.soundName = UILocalNotificationDefaultSoundName

    switch(frequencysegmentedcontrol.selectedSegmentIndex){
    case 0:
        notifications.repeatInterval = NSCalendarUnit.CalendarUnitDay
        break;
    case 1:
        notifications.repeatInterval = NSCalendarUnit.CalendarUnitWeekOfYear
        break;
    case 2:
        notifications.repeatInterval = NSCalendarUnit.CalendarUnitMonth
        break;
    default:
        notifications.repeatInterval = NSCalendarUnit.allZeros
        break;

    }
     notifications.userInfo = ["UUID": "NotificationID"]

    notifications.alertBody = "quote of the day"





  // if user has not confirmed the notification permission
          let settings = UIApplication.sharedApplication().currentUserNotificationSettings()

    if settings.types == .None {
        let ac = UIAlertController(title: "Can't schedule", message: "Either we don't have permission to schedule notifications, or we haven't asked yet.", preferredStyle: .Alert)
        ac.addAction(UIAlertAction(title: "OK", style: .Default, handler: nil))
        presentViewController(ac, animated: true, completion: nil)
        return
    }

    UIApplication.sharedApplication().scheduleLocalNotification(notifications)

最佳答案

请注意,使用重复间隔重新安排本地通知不会取消之前安排的通知。这样一来,如果您已安排每日通知不止一次,通知将每天触发不止一次。要解决此问题,您必须通过调用以下方式手动取消所有先前安排的通知:

UIApplication.sharedApplication().cancelAllLocalNotifications()

关于ios - 本地通知 - 重复间隔(Xcode,swift2),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31914191/

相关文章:

iphone - 无法获得 friend 的生日

ios - 遇到错误时 Xcode 调试器输出不具体

ios - 找不到选项 '-L/Users/..../TestFlightSDK1.1' 的目录

ios - xcode 上的 Flutter audioplayers 插件错误(未找到模块 'audioplayers')

swift - UIDocument 中的 saveToURL 返回 false

ios - 带有 UITableViewCells + AutoLayout 的 UITableView - 不像*应该*那样平滑

iphone - 设置动画总是出现

ios - 是否可以覆盖 super 属性/方法以使其快速私有(private)化?

swift - Vapor 4 身份验证问题 [用户未经过身份验证]

ios - NSNotification 不在另一个类中工作