Klevison
Klevison

Reputation: 3484

Failed to read values in CFPrefsPlistSource iOS 10

I've updated my Xcode 8 to beta 2 today and I'm trying to share data between App and Today Extension. I'm facing with this log warning:

2016-07-08 18:00:24.732472 ProjetctX[941:42801] [User Defaults] Failed to read values in CFPrefsPlistSource<0x1700f1280> (Domain: group.x.p.t.o, User: kCFPreferencesAnyUser, ByHost: Yes, Container: (null)): Using kCFPreferencesAnyUser with a container is only allowed for System Containers, detaching from cfprefsd

Anyone can help me?

Upvotes: 118

Views: 72391

Answers (16)

Ege
Ege

Reputation: 31

Reporting from 2024 on iOS 17.5, the issue is usually about the App Group identifiers if not always.

If you debug your project on a real device, you have to add a TEAM ID to your group identifiers.

Mine solved with checking the App Groups. Basically you need to:

  1. Be sure you have added the TEAM ID to your group identifier. (exp: group.YOUR_TEAM_ID.com.example.yourapp). You may find your TEAM ID at developer.apple.com/account under Membership tab.
  2. Do not enter the group identifier for your extension manually. If the identifier doesn't show up, try to hit refresh button next to plus button under App Groups capability.
  3. Lastly, be sure to enter the same group identifiers for both your app and extension.

Upvotes: 3

carlynorama
carlynorama

Reputation: 324

I'll add a detail here that I haven't seen inn other answers

Your MacOS AppGroups and iOS AppGroups must live on separate targets. MacOS needs the ID to have that MYTEAMID right at the beginning and iOS needs group.. Xcode 15 autocorrects the number leading appgroup ids to group. leading appgroup ids if iOS anything is on the target at all. You can't just #if os(macOS) in your code to pick which one you want, they must be separate targets with separate entitlement files.

Upvotes: 1

wanted
wanted

Reputation: 95

I had this problem before. The problem is the entitlements files. The problem occurs when you change the developer account.

So first delete all the entitlements files, then go to:

Targets > Signing & Capabilities

and disable and re-enable one of the required accesses to create files automatically.

Upvotes: -2

Sand&#39;sHell811
Sand&#39;sHell811

Reputation: 388

This issue mainly comes if the same AppGroupId apps are installed with different bundle identifiers on one device. One app has Staging, Prod, and Test environments and each environment has a different bundle identifier, but all 3 domain has the same appGroudId. So if all environment apps are installed in a single device, then UserPreferences conflicts due to the same AppGroupId.

We have seen this issue with Notification Service Extension and if we install only one environment build then it will work fine.

Upvotes: 3

Daniel
Daniel

Reputation: 1

Set by example

NSUserDefaults *userDefaults = [[NSUserDefaults alloc] initWithSuiteName:@"group.com.xxx.xxx"];
[userDefaults setValue:@"value" forKey:@"key"]
[userDefaults synchronize]; // return maybe false, but it doesn't matter

Get by

NSUserDefaults *userDefaults = [[NSUserDefaults alloc] init];
[userDefaults addSuiteNamed:@"group.com.xxx.xxx"];
NSString *value = [useDefaults valueForKey:@"key"];

Although the same error will still be printed when setting, the value is indeed set and can be read correctly. But I don't know why this is happening, it's just the result of various attempts.

Upvotes: 0

NFlows
NFlows

Reputation: 190

By default, if you are using the Settings.Bundle/Root.plist for displaying and editing your app preferences via Apple Settings App, it uses the UserDefaults.standard dictionary.

So if you are using App-Groups and you want to share this defaults / settings within your apps and extension, you need to change the container of your settings.

Step 1: Open your Settings.Bundle -> Root.plist

Open Settings.Bundle -> Root.plist

Step2: Add the key ApplicationGroupContainerIdentifier and as value set your App-Group-Id, defined in your Signing & Capabilities: Looks like group.xx.yy

Add Key-Value pair for ApplicationGroupContainerIdentifier

After you have implemented this step, the default container for your App-Settings will now switch from UserDefaults.standard (your apps Path) to the Shared Path.

Upvotes: 0

Arfan Mirza
Arfan Mirza

Reputation: 696

Here’s how to use UserDefaults with App Groups to pass data between your main app and your extension:

  1. In your main app, select your project in the Project Navigator.

  2. Select your main app target and choose the Capabilities tab.

  3. Toggle the App Groups switch to ON. This will communicate with the Developer Portal in order to generate a set of entitlements.

  4. Create a new container. According to Apple, your container ID must start with "group", so a name such as "group.io.intrepid.myapp" is perfect.

  5. Select your extension target and repeat the process of enabling App Groups. Do not create a new App Group, simply select the group that was just created in the main app target.

  6. When reading or writing UserDefaults in either your app or your extension, do not access UserDefaults.standard. Instead use UserDefaults(suiteName: "group.io.intrepid.myapp"). Note: The suite name is the name of your App Group container created in Step 4.

Make sure, group enable and using same group id for both extension and app capability section!

Credit goes to http://blog.intrepid.io/ios-app-extensions

Upvotes: 24

Vitaly Baum
Vitaly Baum

Reputation: 305

The solution for me was to not use the same identifier for the application Bundle Identifier and the part after "group.".

Say, the app bundle id is "com.app.id", then group id as "group.com.app.id" is causing issues. After I change it to "group.com.app.id.something" it stops.

Upvotes: 7

Santiago
Santiago

Reputation: 598

Change you group name in Xcode entitlements from:

group.com.mycompany.myapp

To

group.MYTEAMID.com.mycompany.myapp

ps: you can find your MYTEAMID in developer.apple.com membership

Upvotes: 18

waltflanagan
waltflanagan

Reputation: 1512

This is actually a spurious warning that was introduced in iOS 10 and macOS 10.12:

NSUserDefaults tip: in the current OSs there's a logged error "…with a container is only allowed for System Containers…".

This is spurious.

Trying to catch a particular failure mode, caught a normal operation case at the same time.

My successor on UserDefaults also has not figured out a way to make this less alarming without making the symptomatic case impossible to debug :/

https://twitter.com/Catfish_Man/status/784460565972332544 [thread]

The advice of prepending your team ID will silence the warning, but will also create a new empty user defaults. This will result in any previously stored data being unreadable.

For the time being, the solution is just to ignore it.

Also, Apple staff member CFM on the forums:

The logged message is spurious unless you're doing very specific things that I don't think are possible without using private functions (it was added to catch misuse of those functions, but unfortunately also caught a normal usage case).

Upvotes: 107

Emmanuel Sellier
Emmanuel Sellier

Reputation: 611

The SuiteName (postfix) must not be the main Bundle ID.

Upvotes: 6

Jxtt
Jxtt

Reputation: 137

Also had same issue with my macOS app.

Solved it by: Reboot the device!

https://stackoverflow.com/a/39876271

Upvotes: 5

H u Y an g
H u Y an g

Reputation: 23

if you suffer this problem when you try to save data to extension APP by using userDefault, maybe you had written this code:

[[NSUserDefaults standardUserDefaults] initWithSuiteName:@"group.xxx.com"];

This code reset default userDefault.

Actually,the correct code is:

[[NSUserDefaults alloc] initWithSuiteName:@"group.xxx.com"];

http://www.jianshu.com/p/e782104c3bc3

Upvotes: -4

Sagar D
Sagar D

Reputation: 2618

I’m facing this same issue when I’m trying to use initWithSuiteName. Seems that this is a bug from Apple. The only solution / workaround I found is to reset all the settings of the device. Go to Settings -> General -> Reset -> Reset All Settings.

This doesn’t erase any content on the iPhone, just erases all the settings. After resetting the setting, everything worked fine. Let me know if it helps you too.

Upvotes: 1

raz0r
raz0r

Reputation: 3372

Build with Xcode 8.1 Beta and you will see the same warning, but you will also get the value.

Upvotes: 0

Titan
Titan

Reputation: 27

Change from

[[NSUserDefaults alloc] initWithSuiteName:@"group.com.xxx.xxx"];

to

[[NSUserDefaults alloc] initWithSuiteName:@"nnnnnnnnnn.group.com.xxx.xxx"]; 

Where nnnnnnnnn is your team number, the one which you use to sign your code.

Tested under Xcode 8 GM and iOS 10 GM, and worked!

Upvotes: -11

Related Questions