I have a cell in a table view which has a button for scheduling notifications. Is it possible to delete or cancel a notification for that particular cell without affecting the notifications scheduled by other cells ? I want to delete or cancel the particular notification using the same button. If so, please help me with a sample swift code.
@IBAction func setNotification(sender: UIButton!) {
cell = tableView.cellForRowAtIndexPath(NSIndexPath(forRow: sender.tag, inSection: 0)) as! CustomTableViewCell
if sender.selected {
sender.selected = false
}else {
currentRegion = CLCircularRegion(center: CLLocationCoordinate2D(latitude: latitude ,longitude:longitude), radius: radius, identifier: identifier)
regionMonitor()
manager.stopUpdatingLocation()
sender.selected = true } }
code for didEnterRegion()
func locationManager(manager: CLLocationManager!, didEnterRegion region: CLRegion!) {
localNotification.regionTriggersOnce = true
localNotification.alertBody = "you have reached"
UIApplication.sharedApplication().scheduleLocalNotification(localNotification)
NSLog("Entering region") }
You could save a unique identifier for each UILocalNotification
in the userInfo
and then loop through them all to find the unique id and delete it:
var app:UIApplication = UIApplication.shared
if let localNotifications = app.scheduledLocalNotifications {
for oneEvent in localNotifications {
var notification = oneEvent as UILocalNotification
if let userInfoCurrent = notification.userInfo as? [String:AnyObject],
let uid = userInfoCurrent["uid"] as? String {
if uid == uidtodelete {
//Cancelling local notification
app.cancelLocalNotification(notification)
break
}
}
}
}
Updated to Swift 3 and improved to prevent nil
crashes