1

I'm trying to use App Groups to share data between a VPN Extension and it's container application in iOS.

I've been following Apple's instructions on how to do this, but it doesn't seem to be working.

I've created the App Group on my Apple Developer profile with name group.com.fisc.vpnapp.datashare. (The group prefix was automatically prepended to the identifier by Apple).

I then use this code to set a value in my VPN Extension's code

- (void) setStatus:(int) status 
{
    NSUserDefaults *dataShare = [[NSUserDefaults alloc] initWithSuiteName:@"group.com.fisc.vpnapp.datashare"];
    [dataShare setObject:@(status) forKey:@"status"];
    [dataShare synchronize];
}

Then inside of my container application, I'm using the following code to retrieve the value.

/**
 * Retrieve the status of the VPN Connection.
 *
 * @return int
 */
- (int) getStatus 
{
    NSUserDefaults *providerData = [[NSUserDefaults alloc] initWithSuiteName:@"group.com.fisc.vpnapp.datashare"];

    if ([providerData objectForKey:@"status"] != NULL) {
        long providerStatus = [providerData integerForKey:@"status"];

        // Logger is just a class that wraps
        // around NSLog to supply headers and
        // other information.
        [Logger info:[NSString stringWithFormat:@"Found status in shared provider: %@", @(providerStatus)]];

        // Validate that the stored status is within the
        // range of valid status values.
        if 
        (
            providerStatus >= VPN_STATUS_DISCONNECTED && 
            providerStatus <= VPN_STATUS_CONNECTED
        ) {
            return (int)providerStatus;
        }
    }

    // Default to disconnected status.
    return VPN_STATUS_DISCONNECTED;
}

But, it seems that nothing is ever read back from the App Group.

When I try to read the value back in my container application, I get the following message.

[User Defaults] Failed to read values in CFPrefsPlistSource<0x1c4116c80> (Domain: group.com.fisc.vpnapp.datashare, User: kCFPreferencesAnyUser, ByHost: Yes, Container: (null), Contents Need Refresh: Yes): Using kCFPreferencesAnyUser with a container is only allowed for System Containers, detaching from cfprefsd 

I can get rid of that message by changing the string representation of the App Group in my code to group.com.XXXXXXX.fisc.vpnapp.datashare (where XXXXXXXX is my Team ID), it gets rid of the message but it still fails to access my shared data.

I've also tried putting all code that's accessing the App Group through NSUserDefaults on each of the processes respective main threads with no difference.

Edit

I've also tried synchronizing the NSUserDefaults prior to reading from them.

jscs
  • 63,694
  • 13
  • 151
  • 195
Nathan F.
  • 3,250
  • 3
  • 35
  • 69
  • You've verified that the extension actually does the write? – jscs Jun 23 '17 at 00:11
  • @JoshCaswell I'm not sure how to do that besides reading it back – Nathan F. Jun 23 '17 at 00:50
  • Set a breakpoint in the method via Xcode's GUI. Run the app; in the Debug menu use "Attach to process..." and put in the target name of your extension. Then trigger the extension -- assuming it's on-demand -- (open a URL in the browser) and make sure the method runs. – jscs Jun 23 '17 at 01:03

0 Answers0