如何将文件添加到主包的/ Library/Sounds目录中?

Cym*_*ric 9 xcode apple-push-notifications ios

根据Apple的文档,〜/ Library/Sounds中的声音文件将在尝试播放声音时由系统搜索.如何将声音文件添加到此文件夹?

我试图在运行时写入该文件夹,但没有权限.我在xcode中添加了一个Library/Sounds文件夹,但它似乎没有复制过来.

在此输入图像描述

xcode - > window - > devices,选择我的应用程序并显示容器,文件夹不存在 在此输入图像描述

要添加一些上下文,我正在为解析推送通知做自定义声音.服务器人告诉我,当向许多用户广播时,为每个用户在有效载荷中发送自定义声音字符串将非常困难.作为一种解决方法,我试图使用单个声音文件,每次用户选择新声音时都会动态覆盖该声音文件.

因此,声音文件需要由系统自动检测,并且可以在运行时由应用程序修改.将文件添加到主包不起作用,因为它是只读的.我希望〜/ Library/Sound中的文件可以编辑.

我不知道如何继续这一点.任何帮助将不胜感激.

更新:我错误地尝试使用在运行时创建目录

        try fileManager.createDirectoryAtPath("Library/Sounds", withIntermediateDirectories: true, attributes: nil)
Run Code Online (Sandbox Code Playgroud)

应该是正确的代码

        let libraryDir = NSSearchPathForDirectoriesInDomains(NSSearchPathDirectory.LibraryDirectory, NSSearchPathDomainMask.UserDomainMask, true)
        let directoryPath = "\(libraryDir.first!)/Sounds"
        try fileManager.createDirectoryAtPath(directoryPath, withIntermediateDirectories: true, attributes: nil)
Run Code Online (Sandbox Code Playgroud)

我对自定义声音问题的解决方案.

小智 6

我也为此苦苦挣扎。您必须以编程方式创建 Library/Sounds 目录并将您的自定义声音移动到其中。

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)")
}
Run Code Online (Sandbox Code Playgroud)


All*_*len 3

您拥有的链接适用于Mac!适用于iOS的正确文档应该在这里

总之,您只需将声音文件作为应用程序包的非本地化资源添加到项目中.

  • 该链接已过时。404页面。 (8认同)