Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Values not always persisted in App group between companion app & app extension

From time to time, but not always (I have had this working for a bit), the app/extension gets in a state where I can't read a flag set in my App Group between my companion app and my app extension. Don't know how it gets in this state or why the values differ, but it's critical to my application these always be in sync.

Companion app viewDidLoad:

NSUserDefaults *myAppSettings = [[NSUserDefaults alloc] initWithSuiteName:@"group.myapp"];

.....

[myAppSettings setBool:true forKey:@"myBool"];

[myAppSettings synchronize];

NSLog([myAppSettings boolForKey:@"myBool"] ? @"Companion app - bool TRUE" : @"Companion app - bool FALSE");

App extension viewDidLoad

NSUserDefaults *myAppSettings = [[NSUserDefaults alloc] initWithSuiteName:@"group.myapp"];

[myAppSettings synchronize];

NSLog([myAppSettings boolForKey:@"myBool"] ? @"App extension app - bool TRUE" : @"App extension - bool FALSE");

Console output

Companion app - bool TRUE
App extension - bool FALSE

I also synchronize before my companion app will enter background. I have my app group set up in the portal etc.

What am I doing wrong?

EDIT

Apparently others having this problem too: https://devforums.apple.com/message/977151#977151

"I think that this is currently very glitchy.

Sometimes the data sharing works, then a change and all of a sudden the widget can't see the shared data anymore (both on Simulator and device).

Annoying and hope it's a bit more reliable in next beta!"

EDIT 2 Looks like another person has reported this exact issue as well:

"I also noticed the same thing too.This not only happen to the NSUserDefaults, but also all the files in the container folder. The keyboard extension suddenly will lose read/write pemission to the container folder after using the keyboard for a while."

EDIT 3 More evidence: https://devforums.apple.com/message/1028078#1028078

After I upgrade to beta 3, I noticed that sometimes the keyboard failed to open the database because it failed to access to the DB file. The keyboard has been able to access to the file before.

enter image description hereenter image description here

EDIT 4

Seems like this could be because the keyboard loses the RequestsOpenAccess flag. But I can't reproduce it, and there's no way for me to tell for sure.

EDIT 5

Seems like others are reporting this in the iOS8 GM build:

This issue still persists for me in the GM. It seems related to a keyboard crash.. but also there seems to be some contention between keyboard and containing app in terms of who creates the suite in what order. I think this problem is on Apple's end. Trust me, I WANT it to be my fault but I've spent countless hours with trial and error. No matter what I do in code and verify with NSLog, it will end up in this state eventually. Hoping someone finds a magic pill. :S

Has anyone solved this yet?

like image 635
barfoon Avatar asked Aug 07 '14 15:08

barfoon


1 Answers

You must request open access in order to access shared NSUserDefaults. It's stated directly in the App Extension Programming guide:

By default, a keyboard has no network access and cannot share a container with its containing app. To enable these things, set the value of the RequestsOpenAccess Boolean key in the Info.plist file to YES.

Be sure you change the RequestsOpenAccess field to YES. You'll find it in keyboard's Info.plist > NSExtension > NSExtensionAttributes > RequestOpenAccess. Then remove the keyboard in Settings, delete the app, run it again, and add the keyboard again. After you add it, tap on the keyboard name and then flip the switch to enable Allow Full Access. You'll need to instruct the users to follow those same steps to grant access (and reassure them you're not evil), otherwise it simply will not work and you'll never get the data that's stored in your shared container. Note that in iOS 8.3+, if the user hasn't enabled full access the keyboard will be able to access the shared container, but writing to it will not save the data, for security and privacy purposes. In 8.2- you can't access that data without open access granted.

like image 56
Jordan H Avatar answered Oct 30 '22 06:10

Jordan H