如何在swift中取消特定的UILocalNotification?

sum*_*esh 3 iphone uitableview ios uilocalnotification swift

我在表视图中有一个单元格,其中有一个用于安排通知的按钮.是否可以删除或取消该特定单元格的通知而不影响其他单元格安排的通知?我想使用相同的按钮删除或取消特定通知.如果是这样,请帮我提供一个示例快速代码.

@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 } }
Run Code Online (Sandbox Code Playgroud)

代码 didEnterRegion()

func locationManager(manager: CLLocationManager!, didEnterRegion region: CLRegion!) {
            localNotification.regionTriggersOnce = true
            localNotification.alertBody = "you have reached"
            UIApplication.sharedApplication().scheduleLocalNotification(localNotification)
            NSLog("Entering region") }
Run Code Online (Sandbox Code Playgroud)

Rol*_*som 10

你可以保存每个唯一标识符UILocalNotificationuserInfo,然后遍历他们都找到了唯一的ID,并将其删除:

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
                }
            }
        }
    }
Run Code Online (Sandbox Code Playgroud)

更新到Swift 3并进行了改进以防止nil崩溃