Reputation: 13
I am pretty new to programming and completely self taught, so feel free to point out errors in logic or best practice.
I am trying to update a setting in the settingsViewController of the parent app with a toggle button in my watch app. If i use app groups and NSUserDefaults then i can get the settings to update whenever the settingsViewContoller appears by checking defaults. But i need the app to update live as the user toggles the switch in the watch app. I have tried using openParentApplication in the watchSettingsViewController.
- (IBAction)soundToggle:(BOOL)value {
if(!value){
NSDictionary * soundToggleOff = [[NSDictionary alloc]initWithObjects: @[@"soundToggleOff"] forKeys: @[@"toggleState"]];
[WKInterfaceController openParentApplication:soundToggleOff reply:^(NSDictionary *replyInfo, NSError *error) {
NSLog(@"Sound reply is %@", replyInfo );
}];
}else{
NSDictionary * soundToggleOn = [[NSDictionary alloc]initWithObjects: @[@"soundToggleOn"] forKeys: @[@"toggleState"]];
[WKInterfaceController openParentApplication:soundToggleOn reply:^(NSDictionary *replyInfo, NSError *error) {
NSLog(@"Sound reply is %@", replyInfo );
}];
}
Then in the appDelagate for the parent app I am using:
-(void)application:(UIApplication *)application handleWatchKitExtensionRequest:(NSDictionary *)userInfo reply:(void (^)(NSDictionary *))reply{
NSString * request = [userInfo objectForKey:@"toggleState"];
NSString * notificationStatus = request;
SettingsViewController * settingsView = [[SettingsViewController alloc]init];
if ([request isEqualToString:@"soundToggleOn"]) {
[settingsView updatePushNotificationSettings: settingsView.soundButton];
}else if ([request isEqualToString:@"soundToggleOff"]){
[settingsView updatePushNotificationSettings: settingsView.soundButton];
}
NSDictionary * replyContent = [[NSDictionary alloc]initWithObjects:@[notificationStatus] forKeys:@[@"notificationStatus"]];
reply(replyContent);
}
But in the parent app the settings don't change. Is there a way to do this? Thanks
Upvotes: 0
Views: 713
Reputation: 37319
If you only need to update a value in NSUserDefaults then I would not bother creating a view controller. Instead, I would just extract the settings saving functionality in a plain class and call its method to update the setting.
Upvotes: 1
Reputation: 4244
In "application:handleWatchKitExtensionRequest:reply:", you are creating a new "SettingsViewController" but never pushing it on screen.
If there is a "SettingsViewController" already on screen, you need to grab the reference to that one and update the "setting" in this view controller.
Upvotes: 0