Reputation: 8942
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:
Why can't I access the bundle data from my phone?
Upvotes: 1
Views: 483
Reputation: 3513
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
Upvotes: -1
Reputation: 396
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
Upvotes: 0