iOS:如何从其UNNotificationServiceExtension中获取应用程序的已保存数据?

iOS:如何从其UNNotificationServiceExtension中获取应用程序的已保存数据?,第1张

概述有没有办法从Notification-Service-Extension实例中获取保存在iOS应用程序Sandbox中的数据?我想在传递contenhandler之前从数据库中获取一些数据. 我从里面测试过 override func didReceive(_ request: UNNotificationRequest, withContentHandler contentHandler: @e 有没有办法从Notification-Service-Extension实例中获取保存在iOS应用程序SandBox中的数据?我想在传递contenhandler之前从数据库中获取一些数据.

我从里面测试过

overrIDe func dIDReceive(_ request: UNNotificationRequest,withContentHandler contentHandler: @escaPing (UNNotificationContent) -> VoID) {            ...            print("NotificationService dIDReceive UNNotificationRequest,contentHandler: \(contentHandler)")            let documentsPath = NSSearchPathForDirectorIEsInDomains(.documentDirectory,.userDomainMask,true)[0]            let filePath = "\(documentsPath)/Database.db"            print("  documentsPath: \(filePath)")            let fileManager = fileManager.default            var isDir : ObjCBool = false            if fileManager.fileExists(atPath: filePath,isDirectory:&isDir) {                if isDir.boolValue {                    print("  file exists and is a directory")                } else {                    print("  file exists and is a NOT a directory")                }            } else {                print("file does not exist")            }...

对我来说,看起来扩展程序有自己的SandBox和自己的文档文件夹.

该应用程序使用文件夹’Application’,而扩展名使用文件夹’PluginKitPlugin’,都在’/ var / mobile / Containers / Data /’中.

更新:似乎无法访问应用程序沙箱容器.

解决方法 从App Extension编程指南,App Extension可以使用应用程序组 Sharing Data with Your Containing App.

读:

if let defaults = UserDefaults(suitename: "my.app.group") {    if let value = defaults.value(forKey: "key") {        NSLog("\(value)")    }}

写:

if let defaults = UserDefaults(suitename: "my.app.group") {    defaults.set("value",forKey: "key")}

> MMWormhole:iOS应用和扩展之间的消息传递.
> Wormhole:iOS应用程序和扩展程序之间传递消息的更优雅方式.

总结

以上是内存溢出为你收集整理的iOS:如何从其UNNotificationServiceExtension中获取应用程序的已保存数据?全部内容,希望文章能够帮你解决iOS:如何从其UNNotificationServiceExtension中获取应用程序的已保存数据?所遇到的程序开发问题。

如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。

欢迎分享,转载请注明来源:内存溢出

原文地址: http://outofmemory.cn/web/1067707.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2022-05-26
下一篇 2022-05-26

发表评论

登录后才能评论

评论列表(0条)

保存