1

I am adding an App Group to my app for sharing a single plist between the app and the watch. I used to copy a plist from the bundle to Documents for when the app first started up. But with the watch I am now trying to convert it to save to the container but it always seems to be empty. The targets have app group enabled, and I am using the right name in my code. What could be going wrong?

Old Way

// COPY PLIST TO DOCUMENTS
NSFileManager *fileManger=[NSFileManager defaultManager];
NSError *error;
NSArray *pathsArray = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory,NSUserDomainMask,YES);

NSString *doumentDirectoryPath=[pathsArray objectAtIndex:0];

NSString *destinationPath= [doumentDirectoryPath stringByAppendingPathComponent:@"badger.com.vacations.plist"];

NSLog(@"plist path %@",destinationPath);
if (![fileManger fileExistsAtPath:destinationPath]){
    NSString *sourcePath=[[[NSBundle mainBundle] resourcePath]stringByAppendingPathComponent:@"badger.com.vacations.plist"];

    [fileManger copyItemAtPath:sourcePath toPath:destinationPath error:&error];
}

New way - not working

// COPY PLIST TO CONTAINER
    NSURL *containerURL = [[NSFileManager defaultManager] containerURLForSecurityApplicationGroupIdentifier:@"group.com.xxxxx.xxx.container"];
    containerURL = [containerURL URLByAppendingPathComponent:@"name.com.data.plist"];
    NSString *destinationPath= containerURL.path;

    NSLog(@"destinationPath %@", containerURL);

    NSString *sourcePath=[[[NSBundle mainBundle] resourcePath]stringByAppendingPathComponent:@"name.com.data.plist"];
    [fileManger copyItemAtPath:sourcePath toPath:containerURL.path error:&error];
malaki1974
  • 1,605
  • 3
  • 16
  • 32
  • stk's answer is good, but I don't see any reason why you shouldn't be able to use a plist file directly. What exactly is happening? No file is written? An empty file is written? Is anything in the `error`? – gregheo Apr 04 '15 at 03:53

1 Answers1

1

You can´t share a plist as a file (or I simply don´t know about this feature), instead you just generate a new NSUserDefaults instance, which is shareable between targets.

Have a look here: https://devforums.apple.com/message/977151#977151

or the Apple documentation

https://developer.apple.com/library/prerelease/ios/documentation/General/Conceptual/ExtensibilityPG/ExtensionScenarios.html

  • In the Apple Member Center, under 'Identifiers > App Groups' register a new App Group (e.g. group.de.myApp.sharedGroup)
  • In the Apple Member Center, under 'Identifiers > App IDs' refresh your App Ids for the targets that need sharing to use the App Groups feature
  • Regenerate all needed Provisioning Profiles and get them into Xcode
  • Back to Xcode: Under 'Capabilities' in each of your targets that need to share data, set 'App Groups' to on and add the previously registered App group.
  • Talk to the shareable NSUserDefaults container like this:

Store stuff:

NSUserDefaults *groupDefaults = [[NSUserDefaults alloc] initWithSuiteName:@"group.de.myApp.sharedGroup"];
[groupDefaults setInteger:1337 forKey:@"testEntry"];
[groupDefaults synchronize];

Read stuff:

NSUserDefaults *groupDefaults = [[NSUserDefaults alloc] initWithSuiteName:@"group.de.myApp.sharedGroup"];
NSInteger testEntry = [groupDefaults integerForKey:@"testEntry"];
NSLog(@"testEntry: %ld", (long)testEntry);
stk
  • 6,311
  • 11
  • 42
  • 58