NSUserDefault change notification handling in watchAppExtension
Asked Answered
H

2

10

I'm creating a  Watch app just to display a value on the watch when the user taps on a table view in the iPhone/host app.

I would like to get a notification the value changes on a shared UserDefault. It is shared between WatchKit app and the iOS (host) app, so when the user makes any changes in the host app I'm expecting to get the notification. I've done the following:

When user do some action in application (host app):

NSUserDefaults *shared = [[NSUserDefaults alloc] initWithSuiteName:@"group.app"];
id object = [self.plantsArray objectAtIndex:[self.plantsTable indexPathForSelectedRow].row];
[shared setObject:object forKey:@"data"];
[shared synchronize];

In the Watchkit extension have registered for the notification:

[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(print) name:NSUserDefaultsDidChangeNotification object:nil];

but unfortunately I'm not getting any notification, has anyone a solution?

Herb answered 6/1, 2015 at 7:51 Comment(3)
Have you enabled App Group for your app and extension ?Cosmography
@Cosmography yes . its enabled for both extension and appHerb
i am able to get the selected data through the userdefault. But what i need is to perform an action at the time of userdefault changeHerb
C
3

I don't think iOS has capability of distributed notifications between app and extension, notifications will not work between both, instead you need to find a way in which both can monitor changes. For example files.

As you already have created group, you can keep a file in the group folder and add a filewatcher in extension, update the file from app, and filewatcher will catch the change, and your work is done.

For filewatcher see code here.

Hope it helps.

Cheers.

Update

Find File watcher Swift version here. Thanks @rivera for adding.

Cosmography answered 6/1, 2015 at 9:50 Comment(3)
Thanks for your suggestion. But can please tell me how to share file in the group ?Herb
@Herb Its simple, you just need to create a file in the group folder, and update it by catching NSUserDefaultNotification in the app only, you access group folder by using [[NSFileManager defaultManager] containerURLForSecurityApplicationGroupIdentifier:GROUPKEY].path;Cosmography
Thanks @Rivera for the addition.. I have updated in my answer if some misses it.Cosmography
R
2

You can try MMWormHole which provides :

  • a channel between the iOS device and the watch which enables you to send data back and forth between them.
  • Also provides a way to do Notifications without having to handle the file monitoring by urself.

Using it ,That will be all the code needed to do notifications in ur app

[self.wormhole passMessageObject:@{@"buttonNumber" : @(1)} identifier:@"button"];

[self.wormhole listenForMessageWithIdentifier:@"button" 
  listener:^(id messageObject) {
    self.numberLabel.text = [messageObject[@"buttonNumber"] stringValue];
}];
Raising answered 19/11, 2015 at 10:3 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.