【发布时间】:2016-05-26 22:20:47
【问题描述】:
根据Apple's documentation,当尝试播放声音时,系统会搜索~/Library/Sounds 中的声音文件。如何将声音文件添加到此文件夹?
我尝试在运行时写入文件夹,但没有权限。我在 xcode 中添加了一个 Library/Sounds 文件夹,但它似乎没有复制过来。
xcode -> 窗口 -> 设备,选择我的应用程序并显示容器,文件夹不存在
为了添加一些上下文,我正在为解析推送通知做自定义声音。服务员告诉我,当向许多用户广播时,在有效载荷中为每个用户发送自定义声音字符串太难了。作为一种变通方法,我尝试使用单个声音文件,每次用户选择新声音时都会动态覆盖该文件。
因此,声音文件需要系统自动检测,并且可以在应用程序运行时进行修改。将文件添加到主包将不起作用,因为它是只读的。我希望 ~/Library/Sound 中的文件是可编辑的。
我现在不知道如何进行。任何帮助将不胜感激。
更新: 我错误地尝试使用
在运行时创建目录 try fileManager.createDirectoryAtPath("Library/Sounds", withIntermediateDirectories: true, attributes: nil)
正确的代码应该是
let libraryDir = NSSearchPathForDirectoriesInDomains(NSSearchPathDirectory.LibraryDirectory, NSSearchPathDomainMask.UserDomainMask, true)
let directoryPath = "\(libraryDir.first!)/Sounds"
try fileManager.createDirectoryAtPath(directoryPath, withIntermediateDirectories: true, attributes: nil)
【问题讨论】:
-
对于任何可更新/可编辑的资源,您应该使用缓存文件夹
-
问题是缓存文件夹中的声音文件不会被苹果检测到推送通知声音
标签: ios xcode apple-push-notifications