需要以编程方式创建plist,以及今天访问小部件扩展目标

问题描述 投票:0回答:1

我需要从主要目标中创建的TodayViewController访问plist。所以我需要创建plist,同时访问Today小部件目标访问权限。

我已经创建了这样的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

}
ios swift today-extension
1个回答
0
投票

我认为您需要将plist文件放在App Group文件夹中:

步骤1:创建应用程序组:项目导航器 - > YOUR_APP_TARGET - >功能 - >激活应用程序组 - >添加应用程序组ID(通常为group.YOUR_APP_BUNDLE)

步骤2:通过激活功能中的应用程序组,在您的窗口小部件目标中添加此应用程序组ID,然后选择此SAME应用程序组ID

第3步:将您的plist保存在App Group文件夹中。使用此代码从TodayViewController和您的主应用程序中检索此文件夹的URL: guard let appGroupURL = FileManager.default.containerURL(forSecurityApplicationGroupIdentifier: YOUR_APP_GROUP_ID) else { return }

希望这可以帮助。

© www.soinside.com 2019 - 2024. All rights reserved.