NSUserDefault更改watchppExtension中的通知处理 [英] NSUserDefault change notification handling in watchppExtension
问题描述
我正在创建一个iwatch应用程序,只是为了在iPhone手机应用程序中点击桌面视图时显示值。
i'm creating a iwatch app just to display a value on watch when clicking on a tableview in iphone app.
我想收到关于共享的用户默认值更改。它在watchkitapp和iphone应用程序之间共享,因此当用户对手机进行任何更改时,我必须收到通知。我已完成以下操作
i would like to get a notification on a shared userdefault value changes . it is shared between watchkitapp and iphone app, so when the user makes any changes in phone i have to get the notification. i have done the following
当用户在手机上执行某些操作时
When user do Some action in applicaton on 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];
已注册通知
[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(print) name:NSUserDefaultsDidChangeNotification object:nil];
但不幸的是我没有收到任何通知,任何人都知道一些解决方案
But unfortunately i'm not getting any notifications, any one know some solution
推荐答案
我不认为iOS在应用和扩展程序之间具有分布式通知
的能力,通知将两者之间没有工作,相反,你需要找到一种方法,两者都可以监控变化。例如文件。
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.
您已经创建了 group
,您可以将文件保存在组文件夹中并添加扩展名为 filewatcher
,从app更新文件, filewatcher
将捕获更改,您的工作已完成。
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.
对于 filewatcher
请参阅代码这里。
希望有所帮助。
干杯。
更新
查找文件观察者Swift版本
此处。感谢 @rivera
添加。
这篇关于NSUserDefault更改watchppExtension中的通知处理的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!