1

In watchOS 2, it seems like you can't access the data from the WatchKit settings bundle from the watch extension itself, because it now runs on the watch instead of the host iPhone. A solution, which was proposed here, was to read the data on the iPhone and then transfer it to the watch.

My problem is, that I cannot only read the data from the watch, but even from my phone. In ViewController.m I have the following code to get the value of a switch:

NSUserDefaults *userDefaults = [[NSUserDefaults alloc] initWithSuiteName:@"group.de.iossoftware.SocialAppDataSharing"];
NSLog(@"%d", [[[NSUserDefaults alloc] initWithSuiteName:@"group.de.iossoftware.SocialAppDataSharing"] boolForKey:@"key_here"]);

It always returns "0", even when the switch in the settings bundle is enabled.

I'm using app groups and I made sure that I use the same identifier in the settings bundle and in the iPhone app: Watch settings bundle .plist

Why can't I access the bundle data from my phone?

Community
  • 1
  • 1
fredpi
  • 8,414
  • 5
  • 41
  • 61
  • Maybe is a dummy question, but have you added [defaults synchronize]; at the end? – CarlosGz Aug 24 '15 at 09:15
  • Nope, that didn't help. – fredpi Aug 24 '15 at 09:19
  • But you can add and save other values like an NSNumber in NSUserdefaults and read it? does the method value for key returns something saving other values than a BOOL? – CarlosGz Aug 24 '15 at 09:29
  • Settings the value one line before and then reading it works properly. Reading from the settings bundle does not work with other types like string, too. It just returns "(null)". – fredpi Aug 24 '15 at 09:44

2 Answers2

0

Try this:

 NSUserDefaults *userDefaults = [[NSUserDefaults alloc] initWithSuiteName:@"group.de.iossoftware.SocialAppDataSharing"];
[userDefaults setValue:@55 forKey:@"myNumber"];   
[userDefaults synchronize];

Execute one time in iOS simulator and then remove it and add:

    NSUserDefaults *userDefaults = [[NSUserDefaults alloc] initWithSuiteName:@"group.de.iossoftware.SocialAppDataSharing"];
NSLog(@"My number: %@",[userDefaults valueForKey:@"myNumber"]

And execute one more time. You should see the value saved before

NSError *error = nil;
NSLog(@"### Directory path: %@",[[NSFileManager defaultManager]contentsOfDirectoryAtPath:[[NSFileManager defaultManager] containerURLForSecurityApplicationGroupIdentifier:@"group.de.iossoftware.SocialAppDataSharing"].path error:&error]);
NSLog(@"Error: %@",error);

And in the last line you will log the file path where the group files are saved. Open it in the finder and you should see under ...../Library/Preferences/ a plist containing the NSUserdefaults created

CarlosGz
  • 396
  • 2
  • 6
  • So, I can now see the NSUserDefaults created but the NSUserDefaults from by bundle can't be found there. – fredpi Aug 24 '15 at 12:39
  • What do you mean with the defaults from your bundle? The [NSUserDefaults standardUserDefaults]; ? – CarlosGz Aug 24 '15 at 13:05
  • Nope, the [[NSUserDefaults alloc] initWithSuiteName:@"group.de.iossoftware.SocialAppDataSharing"]; – fredpi Aug 24 '15 at 13:07
  • Right, you should see a file with name: group.de.iossoftware.SocialAppDataSharing.plist if not, it could be an issue with setting the shared folder – CarlosGz Aug 24 '15 at 13:11
-1

I'm not sure exactly what you are trying to do as your questions isn't clear to me. However, with WatchOS 2, you cannot use shared group containers, you must use Watch Connectivity framework to sync data between the watch and phone. This is similar to the question here: NSUserDefaults not working on Xcode beta with Watch OS2

Community
  • 1
  • 1
rmp
  • 3,503
  • 1
  • 17
  • 26