在主应用程序和今天的小部件macOS之间共享数据 [英] Share data between main app and today widget macOS

查看:655
本文介绍了在主应用程序和今天的小部件macOS之间共享数据的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试在主要的macOS应用程序和我创建的扩展程序之间共享数据.
我看到我必须使用应用程序组"并与"UserDefault(suiteName:"name")"共享数据

I'm trying to share data between my main macOS app and the extension I created.
I saw I have to use the "App Groups" and share the data with "UserDefault(suiteName: "name")"

问题:在主应用程序上打开应用程序组并添加名称后,我在扩展程序上打开了应用程序组,并且列表为空,我看不到我刚刚创建的组吗?

The problem : After turned on App Groups on the main app and added a name, I then turn on App Groups on extension and the list is empty, I don't see the group I'v just created ?

有什么主意吗?

btw:团队简介在应用程序和扩展程序上都是相同的.我试图删除临时文件,清理项目,还原Xcode和计算机.

btw: the team profil is the same on both app and extension. I'v tried to delete temporary file, clean project, restored Xcode and computer.

在iOS项目上,检测到应用组... .
我尝试使用Xcode 9,但存在相同的问题.
关于证书,标识符和在Apples个人资料网站上,您没有适用于macOS的类别应用程序组"吗?
如果我同时手动添加两个添加组名称,则会在输出中出现错误:. [用户默认值]无法读取CFPrefsPlistSource< 0x6000000e4200>中的值(域:726328455Z.test,用户:kCFPreferencesAnyUser,ByHost:是,容器:(空)):使用kCFPreferencesAnyUser仅用于系统容器,与cfprefsd分离.

EDIT 1 : On iOS project the app groups are detected....
EDIT 2 : I tried with Xcode 9 but same problem.
EDIT 3 : On Certificates, Identifiers & Profiles apple website, you don't have the category "App Groups" for macOS, is it deprecated ??
EDIT 4 : If I add both add groups names manually, then I got an error in output :. [User Defaults] Failed to read values in CFPrefsPlistSource<0x6000000e4200> (Domain: 726328455Z.test, User: kCFPreferencesAnyUser, ByHost: Yes, Container: (null)): Using kCFPreferencesAnyUser with a container is only allowed for System Containers, detaching from cfprefsd.

看起来可以正常工作,使用此代码成功保存了数据

UserDefaults(suiteName: "7263xxx55Z.test")!.set(selectedRow, forKey: "selectedRow")
UserDefaults(suiteName: "7263xxx55Z.test")!.synchronize()

并用此代码获取,我得到了先前的错误,但我只是忽略了它

And fetch with this code, I got the previous error but I just ignore it

let selectedRow = UserDefaults(suiteName: "726xxx55Z.test")!.integer(forKey: "selectedRow")

推荐答案

要在macOS项目中的app/app扩展之间共享数据:

1/在主项目目标中,选择功能"并启用应用程序组".

1/ In your Main project target, select Capabilities and enable "App Groups".

2/单击"+"并添加一个新的应用程序组名称,该名称必须以您的团队ID开头(例如643J438K.name.app)

2/ Click on "+" and add a new app group name, it must start with your team ID (e.g. 643J438K.name.app)

3/为扩展应用程序目标重复1/和2/,请确保添加相同的名称

3/ Repeat 1/ and 2/ for your extension app target, be sure to add the same name

4/要保存和获取数据,请使用:

4/ To save and fetch data use :

// Store data
UserDefaults(suiteName: "643J438K.name.app")!.set(yourVariable, forKey: "yourKey")
UserDefaults(suiteName: "643J438K.name.app")!.synchronize()

// Fetch data (integer type here)
UserDefaults(suiteName: "643J438K.name.app")!.integer(forKey: "yourKey")

5/您可以通过打开以下文件来检查是否正确保存了数据:

5/ You can check that your data is correctly saved by opening this file :

/Users/UserName/Library/Group Containers/643J438K.name.app/Preferences/643J438K.name.app.plist

这篇关于在主应用程序和今天的小部件macOS之间共享数据的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆