如何确定 iOS 上的设置何时更改

发布于 2024-09-27 00:37:02 字数 113 浏览 0 评论 0原文

我使用 iPhone 的标准 root.plist 方法创建了一个自定义 Settings.app 捆绑包。我想知道是否有一种方法可以确定用户何时更改我的应用程序中的这些设置......

I have created a custom Settings.app bundle using the standard root.plist approach for the iPhone. I'm wondering if there's a way to determine when the user changes those settings in my app...

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(7

油饼 2024-10-04 00:37:02

您可以通过以下方式侦听 NSUSerDefaultsDidChange-notifications:

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

每当 NSUserDefaults 更改时,都会调用 defaultsChanged

当你想停止监听这些通知时,不要忘记调用 [[NSNotificationCenter defaultCenter] removeObserver:self];(当对象被释放时,你也应该这样做)。 >

You can listen for NSUSerDefaultsDidChange-notifications with this:

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

Whenever the NSUserDefaults changes, defaultsChanged will be called.

Don't forget to call [[NSNotificationCenter defaultCenter] removeObserver:self]; when you want to stop listening for these notifications (you should also do this when object gets deallocated).

羁客 2024-10-04 00:37:02

语法适用于 Swift 2。使用 Swift,您可以执行类似的操作来订阅 NSUserDefaults 的更改:

NSNotificationCenter.defaultCenter().addObserver(self, selector: "defaultsChanged:", name: NSUserDefaultsDidChangeNotification, object: nil)

然后创建如下方法:

func defaultsChanged(notification:NSNotification){
    if let defaults = notification.object as? NSUserDefaults {
       //get the value for key here
    }
}

The syntax is for Swift 2. Using Swift you would do something like this to subscribe to changes for the NSUserDefaults :

NSNotificationCenter.defaultCenter().addObserver(self, selector: "defaultsChanged:", name: NSUserDefaultsDidChangeNotification, object: nil)

Then create the method like this :

func defaultsChanged(notification:NSNotification){
    if let defaults = notification.object as? NSUserDefaults {
       //get the value for key here
    }
}
椒妓 2024-10-04 00:37:02

SWIFT 4

在viewController中注册观察者,

NotificationCenter.default.addObserver(self, selector: #selector(settingChanged(notification:)), name: UserDefaults.didChangeNotification, object: nil)

Selector实现

 @objc func settingChanged(notification: NSNotification) {
    if let defaults = notification.object as? UserDefaults {
        if defaults.bool(forKey: "enabled_preference") {
            print("enabled_preference set to ON")
        }
        else {
            print("enabled_preference set to OFF")
        }
    }
}

SWIFT 4

Register observer in viewController,

NotificationCenter.default.addObserver(self, selector: #selector(settingChanged(notification:)), name: UserDefaults.didChangeNotification, object: nil)

Selector implementation

 @objc func settingChanged(notification: NSNotification) {
    if let defaults = notification.object as? UserDefaults {
        if defaults.bool(forKey: "enabled_preference") {
            print("enabled_preference set to ON")
        }
        else {
            print("enabled_preference set to OFF")
        }
    }
}
等待我真够勒 2024-10-04 00:37:02

注册以接收 NSUserDefaultsDidChangeNotification 通知。这并不明显,但是 iOS 应用程序编程指南 是这样描述的:

您的应用程序的偏好
通过设置公开
应用程序已更改

Register to receive NSUserDefaultsDidChangeNotification notifications. It's not obvious, but the iOS Application Programming Guide describes it as such:

Preferences that your application
exposes through the Settings
application are changed

浴红衣 2024-10-04 00:37:02

使用键“instantWeb”访问应用程序特定的 Bool 类型设置的示例:

func observeUserDefaults(notification: NSNotification) {
    print("Settings changed")
    if let defaults = notification.object as? NSUserDefaults {
        if defaults.valueForKey("instantWeb") as! Bool==true {
            print("Instant Web ON")
        }
    }
}

An example accessing an app specific Bool type setting with key "instantWeb":

func observeUserDefaults(notification: NSNotification) {
    print("Settings changed")
    if let defaults = notification.object as? NSUserDefaults {
        if defaults.valueForKey("instantWeb") as! Bool==true {
            print("Instant Web ON")
        }
    }
}
怪我入戏太深 2024-10-04 00:37:02

监听设置的变化

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

,一旦该视图控制器不再位于内存中,请记住删除观察者。

Listen to change in settings

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

Remember to remove the observer, once this view controller is no longer in memory.

压抑⊿情绪 2024-10-04 00:37:02

在 iOS10 中,尝试以下操作:

UNUserNotificationCenter.current().getNotificationSettings { (settings) in
    // Your code here
}

In iOS10, try this:

UNUserNotificationCenter.current().getNotificationSettings { (settings) in
    // Your code here
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文