ios - 如何快速取消特定的 UILocalNotification?

标签 ios iphone swift uitableview uilocalnotification

我在表格 View 中有一个单元格,其中有一个用于安排通知的按钮。是否可以删除或取消该特定单元格的通知而不影响其他单元格安排的通知?我想使用相同的按钮删除或取消特定通知。 如果是这样,请帮我提供一个示例 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 } }

didEnterRegion()的代码

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

最佳答案

您可以在 userInfo 中为每个 UILocalNotification 保存一个唯一标识符,然后循环遍历它们以找到唯一 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
                }
            }
        }
    }

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

关于ios - 如何快速取消特定的 UILocalNotification?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32004209/

相关文章:

ios - 错误的接收器类型 'CGImageRef'(又名 'CGImage *')

ios - 使用 GPUImage 归档 ios 应用程序时编译错误

iphone - 为什么 NSNotificationCenter 释放时会抛出异常?

Swift 和 watchkit : pushControllerWithName not being called at all

ios - UIVisualEffectView 变灰(不起作用)

ios - 快速组合 : Buffer upstream values and emit them at a steady rate?

iphone - 涉及NSString的内存泄漏

iphone - 使用 NIB 文件用委托(delegate)实例化 UIView 子类

ios - @lvalue $T 5' is not identical to ' Swift 中的 AnyObject

swift - TapGesture 在 Xcode 11.0 Beta 中不工作