2016-03-25 16 views
0

Bir UIDatePicker kullanarak bildirimleri ayarlamayı başardım, ancak benim sorunum onları başka bir UIDatePicker (datePickerStop) kullanarak durdurmalarını sağlamaktır. Temel olarak, bir Switch nesnesi ve bir if ifadesi içinde iki farklı UIDatePicker ile bildirimleri başlatmak ve sonlandırmak için Schedules. Bu, sahip olduğum bir kod ancak işe yaramıyor gibi görünüyor ya da doğru yere yerleştirmiyorum. Herhangi bir fikrin var mı?UIDatePicker kullanarak belirli bir zamanda bildirimleri durdurma

let notificationArray = UIApplication.sharedApplication().scheduledLocalNotifications! 

for notification in notificationArray { 
    if notification.fireDate == fixedNotificationDate(datePickerStop.date) { 
     UIApplication.sharedApplication().cancelLocalNotification(notification) 
    } 
} 

Bu tam kodu olan

override func viewDidLoad() { 
     super.viewDidLoad() 

     updateUI() 

     let defaults = NSUserDefaults.standardUserDefaults() 

     if (defaults.objectForKey("SwitchState") != nil) { 
      SwitchOutlet.on = defaults.boolForKey("SwitchState") 
     } 
     if (SwitchOutlet.on) { 

      Label1Outlet.text = "Notifications are On" 

     } else { 
      Label1Outlet.text = "Notifications are Off" 

     } 

     // Do any additional setup after loading the view, typically from a nib. 
    } 

    override func didReceiveMemoryWarning() { 
     super.didReceiveMemoryWarning() 
     // Dispose of any resources that can be recreated. 
    } 

    @IBAction func Switch(sender: AnyObject) { 
     let defaults = NSUserDefaults.standardUserDefaults() 

     if (SwitchOutlet.on) { 

      Label1Outlet.text = "Notifications are On" 


      defaults.setBool(true, forKey: "SwitchState") 
      let notification = UILocalNotification() 
      notification.fireDate = fixedNotificationDate(datePicker.date) 
      notification.repeatInterval = NSCalendarUnit.Minute 
      notification.alertBody = “Message” 
      notification.alertAction = “Lock!” 
      notification.soundName = "NotifSound.caf" 
      notification.userInfo = ["CustomField1": "w00t"] 
      guard let settings = UIApplication.sharedApplication().currentUserNotificationSettings() else { return } 

      if settings.types == .None { 
       let ac = UIAlertController(title: "Can't Activate", 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(notification) 



     } else { 

     Label1Outlet.text = "Notifications are Off" 
    defaults.setBool(false, forKey: "SwitchState") 
      UIApplication.sharedApplication().cancelAllLocalNotifications() 


     } 
    } 



    func fixedNotificationDate(dateToFix: NSDate) -> NSDate { 

     let dateComponents: NSDateComponents = NSCalendar.currentCalendar().components([NSCalendarUnit.Day, NSCalendarUnit.Month, NSCalendarUnit.Year, NSCalendarUnit.Hour, NSCalendarUnit.Minute], fromDate: dateToFix) 

     dateComponents.second = 0 

     let fixedDate: NSDate = NSCalendar.currentCalendar().dateFromComponents(dateComponents)! 

     return fixedDate 


} 

    func updateUI() { 

     let currentSettings = UIApplication.sharedApplication().currentUserNotificationSettings() 

     if currentSettings?.types != nil { 

      if currentSettings!.types == [UIUserNotificationType.Badge, UIUserNotificationType.Alert] { 

      } 
      else if currentSettings!.types == [UIUserNotificationType.Badge] { 


      } 
      else if currentSettings!.types == UIUserNotificationType.None { 


      } 

     } 

    } 
} 

cevap

0

let notification:UILocalNotification = UILocalNotification() 
    notification.userInfo = ["day": day, "hour": hour] 
UIApplication.sharedApplication().scheduleLocalNotification(notification) 

planlanan Ve iptal çalışırken sadece UserInfo

ile bulduğunda Eğer bildirime UserInfo ekleyebilir

let app = UIApplication.sharedApplication() 
let day: Int = 1 
let hour: Int = 2 
for singleNotification in app.scheduledLocalNotifications!{ 
    let notDay = singleNotification.userInfo!["day"] as! Int 
    let notHour = singleNotification.userInfo!["hour"] as! Int 
    if(notDay == day && notHour == hour){ 
     app.cancelLocalNotification(singleNotification) 
     break; 
    } 
} 

Int kullanıyordum ama bir sözlük

İlgili konular