How do I add a file to the main bundle's /Library/Sounds directory?

旧巷老猫 提交于 2019-12-03 13:12:41

the link you have is for Mac! the correct document for iOS should be here

in summary, you can just add the sound file to your project as a nonlocalized resource of the app bundle.

I struggled with this as well. You have to create the Library/Sounds directory programmatically and move your custom sound into it.

let soundsDirectoryURL = fileManager.urls(for: .libraryDirectory, in: .userDomainMask).first!.appendingPathComponent("Sounds")

//attempt to create the folder
do {
    try fileManager.createDirectory(atPath: soundsDirectoryURL.path,
                                    withIntermediateDirectories: true, attributes: nil)
} catch let error as NSError {
    print("Error: \(error.localizedDescription)")
}

I had the exact same problem at work. I would suggest -

  1. Have multiple sound files in your project.
  2. Persist user's sound selection.
  3. When you receive push notification, play the sound using what you persisted in step 2.
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!