我需要从主目标中创建的 TodayViewController
访问 plist。所以我还需要创建具有今日小部件目标访问权限的 plist。
我已经创建了这样的plist,但是我需要从今天的扩展目标访问这个plist
let InfoDict:NSDictionary = [
"ParentInfo":"Mathew"
]
DispatchQueue.main.async {
let isWritten = InfoDict.write(toFile: self.getPlistPath(), atomically: true)
print("is the file created: \(isWritten)")
}
func getPlistPath()->String{
let fileManager = FileManager.default
let documentDirectory = NSSearchPathForDirectoriesInDomains(.documentDirectory, .userDomainMask, true)[0] as String
let path = documentDirectory.appending("/UserConfig.plist")
if(!fileManager.fileExists(atPath: path)){
self.saveGlobalValuesToPlist()
}
return path
}
最佳答案
我认为您需要将 plist 文件放在 App Group 文件夹中:
第 1 步:创建应用程序组:项目导航器 -> YOUR_APP_TARGET -> 功能 -> 激活应用程序组 -> 添加应用程序组 ID(通常为 group.YOUR_APP_BUNDLE)
第 2 步:在您的小部件目标中添加此应用程序组 ID,方法是在功能中激活应用程序组并选择此相同的应用程序组 ID
第 3 步:将您的 plist 保存在 App Group 文件夹中。使用此代码从 TodayViewController
检索此文件夹的 url和你的主应用程序:
guard let appGroupURL = FileManager.default.containerURL(forSecurityApplicationGroupIdentifier: YOUR_APP_GROUP_ID) else {
return
}
希望这有帮助。
关于ios - 需要以编程方式创建 plist 并访问今天的小部件扩展目标,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/54704035/