Ios 本地通知声音不工作

Ios 本地通知声音不工作,ios,swift,audio,ios8,uilocalnotification,Ios,Swift,Audio,Ios8,Uilocalnotification,我正在尝试制作一个报警应用程序,但当我尝试制作一个本地通知时,我无法播放声音。我得到了这个错误: [user info=(null)}具有声音,但尚未收到用户播放声音的权限] 代码如下: @IBOutlet var setAlaram: UIDatePicker! @IBAction func setAlarmButton(sender: AnyObject) { var dateformater = NSDateFormatter() dateformater.timeZ

我正在尝试制作一个报警应用程序,但当我尝试制作一个本地通知时,我无法播放声音。我得到了这个错误:

[user info=(null)}具有声音,但尚未收到用户播放声音的权限]

代码如下:

@IBOutlet var setAlaram: UIDatePicker!


@IBAction func setAlarmButton(sender: AnyObject) {

    var dateformater = NSDateFormatter()
    dateformater.timeZone = NSTimeZone .defaultTimeZone()
    dateformater.timeStyle = NSDateFormatterStyle.ShortStyle
    dateformater.dateStyle = NSDateFormatterStyle.ShortStyle

    var datetimestring = NSString()
    datetimestring = dateformater.stringFromDate(setAlaram.date)
    println(datetimestring)

    [self .localnotification(setAlaram.date)]

}

func localnotification (firedate:NSDate)  {

    var localNotification:UILocalNotification = UILocalNotification()
    localNotification.fireDate = firedate
    localNotification.alertBody = "time to woke up"
    localNotification.soundName = "alarm.wav"
    UIApplication.sharedApplication().scheduleLocalNotification(localNotification)

}

override func viewDidLoad() {
    super.viewDidLoad()
    let date1 = NSDate()
    setAlaram.date = date1

}

您需要向用户请求在iOS 8中发送本地通知的权限

您可以在应用程序在AppDelegate方法中启动时执行此操作

func application(application: UIApplication, didFinishLaunchingWithOptions launchOptions: [NSObject: AnyObject]?) -> Bool {
    application.registerUserNotificationSettings(UIUserNotificationSettings(forTypes: .Sound | .Alert | .Badge, categories: nil))
    return true
}

在sbarow的帮助下发布最终答案,我发现这里的解决方案是代码,它可能对其他人有帮助

1.AppDelegate.swift替换此:

func application(application: UIApplication, didFinishLaunchingWithOptions launchOptions: [NSObject: AnyObject]?) -> Bool {
    application.registerUserNotificationSettings(UIUserNotificationSettings(forTypes: .Sound | .Alert | .Badge, categories: nil))
    return true
}
2.ViewController.swift:

@IBOutlet var setAlaram: UIDatePicker!


@IBAction func setAlarmButton(sender: AnyObject) {

    var dateformater = NSDateFormatter()
    dateformater.timeZone = NSTimeZone .defaultTimeZone()
    dateformater.timeStyle = NSDateFormatterStyle.ShortStyle
    dateformater.dateStyle = NSDateFormatterStyle.ShortStyle

    var datetimestring = NSString()
    datetimestring = dateformater.stringFromDate(setAlaram.date)
    println(datetimestring)

    [self .localnotification(setAlaram.date)]
}

func localnotification (firedate:NSDate)  {
    var localNotification:UILocalNotification = UILocalNotification()
    localNotification.fireDate = firedate
    localNotification.alertBody = "time to woke up"
    localNotification.soundName = "alarm.wav"

    UIApplication.sharedApplication().scheduleLocalNotification(localNotification)
}

override func viewDidLoad() {
    super.viewDidLoad()

    let date1 = NSDate()
    setAlaram.date = date1
}

感谢您的帮助,我现在收到通知但没有播放声音?您有自定义声音吗?我的建议是设置
localNotification.soundName=UILocalNotificationDefaultSoundName
,然后查看声音是否正常。如果是这样,那么你就知道这是你的自定义声音。我发现解决方案是在真实设备上工作,而不是在模拟器上。我仍然可以使用localNotification.soundName=“alarm.wav”,但感谢你尝试帮助我:)将
localNotification
用作函数名有点令人困惑。。。