ios - 本地通知声音不起作用

标签 ios swift audio ios8 uilocalnotification

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

[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
}

关于ios - 本地通知声音不起作用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/26734350/

相关文章:

c++ - 是否可以在不重新启动音频流的情况下添加和/或替换双簧管中的音频源?

ios - 通过video-graph.facebook iOS将视频上传到Facebook

ios - 在单元测试中,将模拟值设置为参数(按引用传递)

iphone - 横向的 UIImagePickerController

ios - Swift 屏蔽 UIView 约束问题

c - 在 PulseAudio 中播放具有不同 sample_spec 的样本

ios - 在 Swift 3 iOS 8 应用程序中集成 GIF

objective-c - TouchID 在某些 iPhone 5S 设备上崩溃

ios - UIFont 等宽数字+小型大写字母

android - Android 上 native 代码中的音频捕获/渲染