使用 Swift 2 的 iOS 9 本地通知

Posted

技术标签:

【中文标题】使用 Swift 2 的 iOS 9 本地通知【英文标题】:iOS 9 Local Notifications using Swift 2 【发布时间】:2015-11-12 10:47:35 【问题描述】:

我有以下代码

@IBAction func triggerNotif(sender: UIButton) 
    let dateComp:NSDateComponents = NSDateComponents()
    dateComp.year = 2015
    dateComp.month = 11
    dateComp.day = 12
    dateComp.hour = 21
    dateComp.minute = 30
    dateComp.timeZone = NSTimeZone.systemTimeZone()

    let calender:NSCalendar = NSCalendar(calendarIdentifier: NSCalendarIdentifierGregorian)!
    let date:NSDate = calender.dateFromComponents(dateComp)!

    let notification:UILocalNotification = UILocalNotification()
    notification.alertBody = "body of notification"
    notification.fireDate = date
    notification.repeatInterval = NSCalendarUnit.Day

    UIApplication.sharedApplication().scheduleLocalNotification(notification)

基本上,我正在尝试使用 UIButton 触发每天重复的通知。我参考了这个网站上的许多其他问题来形成这段代码,并且它构建成功。

但是当我点击按钮时,我遇到了这样的问题:

2015-11-12 18:30:17.025 Application Name[95723:485982] -[Application_Name.SettingsViewController triggerNotifBtn:]: unrecognized selector sent to instance 0x7f9163727d70
2015-11-12 18:30:17.035 Application Name[95723:485982] *** Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: '-[Application_Name.SettingsViewController triggerNotifBtn:]: unrecognized selector sent to instance 0x7f9163727d70'
*** First throw call stack:
(
    0   CoreFoundation                      0x000000010d293f45 __exceptionPreprocess + 165
    1   libobjc.A.dylib                     0x000000010efb7deb objc_exception_throw + 48
    2   CoreFoundation                      0x000000010d29c56d -[NSObject(NSObject) doesNotRecognizeSelector:] + 205
    3   CoreFoundation                      0x000000010d1e9eea ___forwarding___ + 970
    4   CoreFoundation                      0x000000010d1e9a98 _CF_forwarding_prep_0 + 120
    5   UIKit                               0x000000010dab1e91 -[UIApplication sendAction:to:from:forEvent:] + 92
    6   UIKit                               0x000000010dc1d4d8 -[UIControl sendAction:to:forEvent:] + 67
    7   UIKit                               0x000000010dc1d7a4 -[UIControl _sendActionsForEvents:withEvent:] + 311
    8   UIKit                               0x000000010dc1c8d4 -[UIControl touchesEnded:withEvent:] + 601
    9   UIKit                               0x000000010db1fed1 -[UIWindow _sendTouchesForEvent:] + 835
    10  UIKit                               0x000000010db20c06 -[UIWindow sendEvent:] + 865
    11  UIKit                               0x000000010dad02fa -[UIApplication sendEvent:] + 263
    12  UIKit                               0x000000010daaaabf _UIApplicationHandleEventQueue + 6844
    13  CoreFoundation                      0x000000010d1c0011 __CFRUNLOOP_IS_CALLING_OUT_TO_A_SOURCE0_PERFORM_FUNCTION__ + 17
    14  CoreFoundation                      0x000000010d1b5f3c __CFRunLoopDoSources0 + 556
    15  CoreFoundation                      0x000000010d1b53f3 __CFRunLoopRun + 867
    16  CoreFoundation                      0x000000010d1b4e08 CFRunLoopRunSpecific + 488
    17  GraphicsServices                    0x0000000111887ad2 GSEventRunModal + 161
    18  UIKit                               0x000000010dab030d UIApplicationMain + 171
    19  Application Name                            0x000000010d0a8b6d main + 109
    20  libdyld.dylib                       0x000000010fabf92d start + 1
)
libc++abi.dylib: terminating with uncaught exception of type NSException
(lldb) 

怎么了?

【问题讨论】:

阅读错误信息了吗?显然,您的函数名称 (triggerNotif) 和无法识别的选择器 (triggerNotifBtn) 之间存在差异,这意味着您可能在未重新连接界面生成器中的操作的情况下重命名了该函数。 – 这个问题与本地通知无关 看来您最初将 ibaction 命名为 triggerNotifBtn 并在您的界面中连接了它。然后将该函数重命名为triggerNotif。这就是为什么你会遇到这样的崩溃。重新连接插座,它会解决问题。 【参考方案1】:

您将 triggerNotifBtn 设置为函数名称,但您的函数名称是 triggerNotif

【讨论】:

以上是关于使用 Swift 2 的 iOS 9 本地通知的主要内容,如果未能解决你的问题,请参考以下文章

Swift 中的本地通知在 iOS 7 上崩溃

UNUserNotificationCenter Swift - 在特定情况下未触发本地通知

swift ios检查本地通知是不是被解除[重复]

如何在 swift [iOS] 中从特定时间开始发送本地通知 UNTimeInterval

来自 URL iOS Swift 的本地通知自定义声音

从 Swift 3 中的锁定屏幕清除本地通知