gpt4 book ai didi

ios - scheduleLocalNotification 在 Swift 2.0 中不起作用

转载 作者:搜寻专家 更新时间:2023-11-01 06:06:37 25 4
gpt4 key购买 nike

我是 iOS 技术的新手。我正在尝试使用 UILocalNotification 使用 swift 2.0 构建一个闹钟应用程序。当我单击按钮时,我从 UIPickerView 获取时间和日期。我也想显示通知和播放声音。但是声音和通知都不起作用。请帮助我!

@IBOutlet weak var myPickerView: UIDatePicker!

override func viewDidLoad() {
super.viewDidLoad()
myPickerView.date = NSDate()
}

override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
// Dispose of any resources that can be recreated.
}


@IBAction func setAlarmClicked(sender: UIButton) {
let dateFormater : NSDateFormatter = NSDateFormatter()
dateFormater.timeZone = NSTimeZone.defaultTimeZone()
dateFormater.timeStyle = .ShortStyle
dateFormater.dateStyle = .ShortStyle

let dateTimeString : NSString = dateFormater.stringFromDate(myPickerView.date)
print("date is \(dateTimeString)")

self.sheduleLocalNotificationWithDate(myPickerView.date)
}

@IBAction func cancelAlarmClicked(sender: UIButton) {

}

func sheduleLocalNotificationWithDate(fireDate : NSDate){
let localNotification : UILocalNotification = UILocalNotification()
localNotification.fireDate = fireDate
localNotification.alertBody = "Alert !!!!"
localNotification.soundName = "minion.mp3"

UIApplication.sharedApplication().scheduleLocalNotification(localNotification)

}

最佳答案

您必须注册本地通知。将此添加到您的 View Controller :

override func viewDidAppear() {

super.viewDidAppear()
let settings = UIUserNotificationSettings(forTypes: [.Badge, .Sound, .Alert], categories: nil)
UIApplication.sharedApplication().registerUserNotificationSettings(settings)

}

当您的 View 第一次出现时会出现警告,点击“确定”,您的通知应该会起作用。请记住,用户始终可以禁用通知。

关于ios - scheduleLocalNotification 在 Swift 2.0 中不起作用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35470751/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com