如何在Swift中使用UILocalNotification
我想弄清楚如何设置一个UILocalNotification迅速,但我没有很多运气。 我正在尝试这个:
var notification = UILocalNotification() notification.timeZone = NSTimeZone.defaultTimeZone() var dateTime = NSDate.date() notification.fireDate(dateTime) notification.alertBody("Test") UIApplication.sharedApplication().scheduleLocalNotification(notification)
对于初学者,我不确定这是否是获取当前date时间的正确方法。 在.Net中,我只是做DateTime.Now()。
其次,当我尝试这个,我得到一个错误 ,说:
'(@lvalue NSDate!) – > $ T3'与'NSDate'不一样
不幸的是,我不知道这意味着什么或如何进行。
首先,你使用初始化语法构造一个NSDate
:
let dateTime = NSDate()
该文档显示了ObjC便捷构造函数如何映射到Swift初始化器。 如果文档显示一个类的init()
,你可以使用类的名称来调用它:对于NSDate
, init()
意味着你调用NSDate()
, init(timeInterval:sinceDate:)
意味着你调用NSDate(timeInterval: x, sinceDate: y)
等
第二: fireDate
不是一个方法,它是一个属性。 你应该分配给它,而不是试图调用它:
notification.fireDate = dateTime
同上alertBody
。
你也可以通过命令在Swift源文件中点击一个类名(或其他API符号)来findCocoa API的Swift语法; 这会导致Xcode生成相关头文件的“Swift-ified”版本。
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) }
不回答你的问题,但值得注意:
notification.fireDate(dateTime) notification.alertBody("Test")
也会抛出一个编译器错误,说它找不到init。 做这个,而不是
notification.fireDate = NSDate(timeIntervalSinceNow: 15) notification.alertBody = "Notification Received"
也支持创builddate如下所示:
NSDate(timeIntervalSinceNow: 15)
在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!") } } }
会很高兴也分离出一些组件:
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()
来安排本地通知。