【问题标题】:How To Use UILocalNotification In Swift如何在 Swift 中使用 UILocalNotification
【发布时间】:2014-07-01 22:11:02
【问题描述】:

我正在尝试弄清楚如何快速设置 UILocalNotification,但我运气不佳。我正在尝试这个:

var notification = UILocalNotification()
notification.timeZone = NSTimeZone.defaultTimeZone()
var dateTime = NSDate.date()
notification.fireDate(dateTime)
notification.alertBody("Test")
UIApplication.sharedApplication().scheduleLocalNotification(notification)

对于初学者,我不确定这是否是获取当前日期时间的正确方法。在 .Net 中,我只会做 DateTime.Now()。

其次,当我尝试这个时,我得到一个 错误,上面写着:

'(@lvalue NSDate!) -> $T3' 与 'NSDate' 不同

不幸的是,我不知道这意味着什么或如何进行。

【问题讨论】:

  • 我也尝试添加 nil 作为firedate的参数。文档说如果它是 nil 它现在应该触发。但是,当我尝试这样做时,我得到了基本相同的错误。

标签: ios swift uilocalnotification


【解决方案1】:

首先,使用初始化语法构造一个NSDate

let dateTime = NSDate()

The documentation 展示了 ObjC 便利构造函数如何映射到 Swift 初始化程序。如果文档显示一个类的init(),您可以使用该类的名称来调用它:对于NSDateinit() 表示您调用NSDate()init(timeInterval:sinceDate:) 表示您调用NSDate(timeInterval: x, sinceDate: y),等等。

第二:fireDate 不是方法,而是属性。您应该分配给它而不是尝试调用它:

notification.fireDate = dateTime

alertBody 同上。

您还可以通过命令单击 Swift 源文件中的类名(或其他 API 符号)来找到 Cocoa API 的 Swift 语法;这会导致 Xcode 生成相关头文件的“Swift-ified”版本。

【讨论】:

    【解决方案2】:
    func setupNotificationReminder() {
        var title:String = "Your reminder text goes here"
    
        let calendar = NSCalendar.currentCalendar()
        let calendarComponents = NSDateComponents()
        calendarComponents.hour = 7
        calendarComponents.second = 0
        calendarComponents.minute = 0
        calendar.timeZone = NSTimeZone.defaultTimeZone()
        var dateToFire = calendar.dateFromComponents(calendarComponents)
    
        // create a corresponding local notification
        let notification = UILocalNotification()
    
        let dict:NSDictionary = ["ID" : "your ID goes here"]
        notification.userInfo = dict as! [String : String]
        notification.alertBody = "\(title)"
        notification.alertAction = "Open"
        notification.fireDate = dateToFire
        notification.repeatInterval = .Day  // Can be used to repeat the notification
        notification.soundName = UILocalNotificationDefaultSoundName
        UIApplication.sharedApplication().scheduleLocalNotification(notification)
    }
    

    【讨论】:

      【解决方案3】:

      不回答你的问题,但值得注意:

      notification.fireDate(dateTime)
      notification.alertBody("Test")
      

      还会抛出一个编译器错误,说它找不到 init。改为这样做

      notification.fireDate = NSDate(timeIntervalSinceNow: 15)
      notification.alertBody = "Notification Received"
      

      【讨论】:

        【解决方案4】:

        还支持像这样创建日期:

        NSDate(timeIntervalSinceNow: 15)
        

        【讨论】:

          【解决方案5】:

          在 Swift 中,使用唯一键取消特定的本地通知:

          func cancelLocalNotification(UNIQUE_ID: String){
          
                  var notifyCancel = UILocalNotification()
                  var notifyArray = UIApplication.sharedApplication().scheduledLocalNotifications
          
                  for notifyCancel in notifyArray as! [UILocalNotification]{
          
                      let info: NSDictionary = notifyCancel.userInfo as! [String : String]
          
                      if info[UNIQUE_ID]!.isEqual(UNIQUE_ID){
          
                          UIApplication.sharedApplication().cancelLocalNotification(notifyCancel)
                      }else{
          
                          println("No Local Notification Found!")
                      }
                  }
              }
          

          【讨论】:

            【解决方案6】:

            最好也分离出一些组件:

            private let kLocalNotificationMessage:String = "Your message goes here!"
            private let kLocalNotificationTimeInterval:NSTimeInterval = 5
            
            private func LocalNotification() -> UILocalNotification {
              var localNotification:UILocalNotification = UILocalNotification()
              localNotification.fireDate = NSDate(timeIntervalSinceNow:kLocalNotificationTimeInterval)
              localNotification.alertBody = kLocalNotificationMessage
              return localNotification
            }
            
            private func ScheduleLocalNotificationIfPossible() {
              if (UIApplication.sharedApplication().isRegisteredForRemoteNotifications()) {
                UIApplication.sharedApplication().scheduleLocalNotification(LocalNotification())
              }
            }
            

            如果用户已注册远程通知,现在您可以致电ScheduleLocalNotificationIfPossible() 安排本地通知。

            【讨论】:

              猜你喜欢
              • 1970-01-01
              • 1970-01-01
              • 1970-01-01
              • 1970-01-01
              • 2014-09-09
              • 1970-01-01
              • 1970-01-01
              • 1970-01-01
              • 1970-01-01
              相关资源
              最近更新 更多