From 99bb686be1458c2da924d67e7ed06815f658cafe Mon Sep 17 00:00:00 2001 From: Fin Date: Wed, 7 Aug 2024 16:31:23 +0800 Subject: [PATCH] =?UTF-8?q?=E6=8C=81=E7=BB=AD=E5=93=8D=E9=93=83=E6=94=AF?= =?UTF-8?q?=E6=8C=81=E8=87=AA=E5=AE=9A=E4=B9=89=E9=93=83=E5=A3=B0?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- Controller/SoundsViewModel.swift | 33 ++++++++++++++++++- .../Processor/CallProcessor.swift | 4 +-- 2 files changed, 34 insertions(+), 3 deletions(-) diff --git a/Controller/SoundsViewModel.swift b/Controller/SoundsViewModel.swift index 7d7d699..53afa4c 100644 --- a/Controller/SoundsViewModel.swift +++ b/Controller/SoundsViewModel.swift @@ -178,22 +178,53 @@ class SoundFileStorage: SoundFileStorageProtocol { /// 将指定文件保存在 Library/Sound,如果存在则覆盖 func saveSound(url: URL) { + // 保存到Sounds文件夹 let soundsDirectoryUrl = getSoundsDirectory() let soundUrl = soundsDirectoryUrl.appendingPathComponent(url.lastPathComponent) try? fileManager.copyItem(at: url, to: soundUrl) + + // 另外复制一份到共享目录 + saveSoundToGroupDirectory(url: url) } func deleteSound(url: URL) { + // 删除sounds目录铃声文件 try? fileManager.removeItem(at: url) + + // 再删除共享目录中对应的铃声文件 + if let groupSoundUrl = getSoundsGroupDirectory()?.appendingPathComponent(url.lastPathComponent) { + try? fileManager.removeItem(at: groupSoundUrl) + } } /// 获取 Library 目录下的 Sounds 文件夹 /// 如果不存在就创建 - func getSoundsDirectory() -> URL { + private func getSoundsDirectory() -> URL { let soundsDirectoryUrl = NSSearchPathForDirectoriesInDomains(.libraryDirectory, .userDomainMask, true).first!.appending("/Sounds") if !fileManager.fileExists(atPath: soundsDirectoryUrl) { try? fileManager.createDirectory(atPath: soundsDirectoryUrl, withIntermediateDirectories: true, attributes: nil) } return URL(fileURLWithPath: soundsDirectoryUrl) } + + /// 保存到共享文件夹,供 NotificationServiceExtension 使用(例如持续响铃需要拿到这个文件) + private func saveSoundToGroupDirectory(url: URL) { + guard let groupUrl = getSoundsGroupDirectory() else { + return + } + let soundUrl = groupUrl.appendingPathComponent(url.lastPathComponent) + try? fileManager.copyItem(at: url, to: soundUrl) + } + + /// 获取共享目录下的 Sounds 文件夹 + /// 如果不存在就创建 + private func getSoundsGroupDirectory() -> URL? { + if let directoryUrl = FileManager.default.containerURL(forSecurityApplicationGroupIdentifier: "group.bark")?.appendingPathComponent("Sounds") { + if !fileManager.fileExists(atPath: directoryUrl.absoluteString) { + try? fileManager.createDirectory(atPath: directoryUrl.absoluteString, withIntermediateDirectories: true, attributes: nil) + } + return directoryUrl + } + return nil + } } diff --git a/NotificationServiceExtension/Processor/CallProcessor.swift b/NotificationServiceExtension/Processor/CallProcessor.swift index d2a8ec7..93fc395 100644 --- a/NotificationServiceExtension/Processor/CallProcessor.swift +++ b/NotificationServiceExtension/Processor/CallProcessor.swift @@ -136,10 +136,10 @@ class CallProcessor: NotificationContentProcessor { func getSoundInCustomSoundsDirectory(soundName: String) -> String? { // 扩展访问不到主APP中的铃声,需要先共享铃声文件,再实现自定义铃声响铃 - guard let soundsDirectoryUrl = NSSearchPathForDirectoriesInDomains(.libraryDirectory, .userDomainMask, true).first?.appending("/Sounds") else { + guard let soundsDirectoryUrl = FileManager.default.containerURL(forSecurityApplicationGroupIdentifier: "group.bark")?.appendingPathComponent("Sounds") else { return nil } - let path = soundsDirectoryUrl.appending("/\(soundName)") + let path = soundsDirectoryUrl.appendingPathComponent(soundName).absoluteString if FileManager.default.fileExists(atPath: path) { return path }