Handling NSUserDefault change notifications in watchppExtension

I am creating an iwatch application to display the value on the watch when I click on a table in the iphone application.

I would like to receive a notification about user sharing changes. It is divided between watchkitapp and iphone app, so when a user makes any changes to the phone, I should get a notification. I did the following

When the user performs some actions in the application by phone

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

registered in watchkit extension for notification

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

But, unfortunately, I do not receive any notifications, does anyone know some solution

+7
ios apple-watch usergroups
source share
2 answers

I don’t think that iOS has the possibility of distributed notifications between the application and the extension, notifications will not work between them, instead you need to find a way by which both can track changes. For example files.

As you have already created group , you can save the file in the group folder and add the filewatcher extension, update the file from the application, and filewatcher will catch the change and your work will be done.

For filewatcher see code here .

Hope this helps.

Greetings.

Update

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

+2
source share

You can try MMWormHole , which provides:

  • A channel between the iOS device and the watch, which allows you to send data between them.
  • Also provides a way to make Notifications without having to handle file monitoring using urself.

Using it, this will be all the code required for notification in the ur application

 [self.wormhole passMessageObject:@{@"buttonNumber" : @(1)} identifier:@"button"]; [self.wormhole listenForMessageWithIdentifier:@"button" listener:^(id messageObject) { self.numberLabel.text = [messageObject[@"buttonNumber"] stringValue]; }]; 
+2
source share

All Articles