我已将通知服务扩展和内容扩展添加到现有应用.服务扩展使用APNS推送通知中传递的URL下载视频文件.然后将其附加到通知中.内容扩展将附件URL传递给AVPlayer,因此如果用户决定查看,则视频将在通知内播放.到现在为止还挺好.现在我想保存下载的视频文件,以便在主视图中查看主视图.为此,我创建了一个应用程序组,为主要目标和内容服务扩展启用了应用程序组功能,并在功能中选择了应用程序组,两者都是.然后,我将下载的文件移动到应用程序组共享容器,而不是将文件保存到服务扩展中的临时目录:
let groupDirectoryUrl = FileManager.default.containerURL(forSecurityApplicationGroupIdentifier: "group.net.mydomain.myapp")!
let groupPathUrl = groupDirectoryUrl.appendingPathComponent("Library/Caches/myapp") // added this later to see if it would work from within the Caches directory
let groupPath = groupPathUrl.path
// Code snipped, creating myapp folder if it does not exist
do {
try FileManager.default.moveItem(at: location, to: groupFileUrl) // location is the url of the downloaded file
self.tmpLogger.addLogEntry(severity: 5, "<File copied to>: \(groupFileUrl)") // This is so I can view log entries from within my app
if (FileManager.default.fileExists(atPath: groupFileUrl.path)) { // …Run Code Online (Sandbox Code Playgroud)