ios - 了解 NSUserDefaults 和 NSUserDefaultsDidChangeNotification

标签 ios notifications settings

有什么方法可以使用 NSUserDefaultsDidChangeNotification 通知我对一般设备设置所做的更改?

我不确定这里指的是什么设置。它是否仅谈论应用程序特定设置或整体设备设置。

我想要做的是:如果用户打开/关闭 wifi 或蓝牙或任何其他设置,我想要一个通知。我可以使用 NSUserDefaultsDidChangeNotification 做到这一点吗?

到目前为止,我能想到的是创建一个自定义通知,每次更改变量的值时都会触发该通知。但是,在这种情况下,我将不得不不断轮询设置的值。

最佳答案

来自 NSUserDefaults class reference :

The NSUserDefaults class provides a programmatic interface for interacting with the defaults system. The defaults system allows an application to customize its behavior to match a user’s preferences. For example, you can allow users to determine what units of measurement your application displays or how often documents are automatically saved. Applications record such preferences by assigning values to a set of parameters in a user’s defaults database. The parameters are referred to as defaults since they’re commonly used to determine an application’s default state at startup or the way it acts by default.



根据您想要监视 wifi 或蓝牙或其他设备设置的操作,不可能为此目的使用 NSUserDefaults。所以是的,你不能使用 NSUserDefaultsDidChangeNotification。它更多地用于您的特定应用程序的用户默认设置。

wifi如何判断:iPhone Wifi on or off?

对于蓝牙可用性,我认为您可以使用 Core Bluetooth .对于其他设备设置,您需要一一查找。

关于ios - 了解 NSUserDefaults 和 NSUserDefaultsDidChangeNotification,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/16872817/

相关文章:

settings - iOS 11+ 中设置>隐私>定位服务的 URL?

ios - CocoaPods:混合 Objective-C 和 Swift

Eclipse 当前成员语法在深色主题中突出显示

iphone - 更新了 NSAttributedString 中的子字符串

java - JavaFX 可以 native 显示操作系统通知吗?

python - 当新用户加入门户时,Plone 中是否有一种简单的方法来获取电子邮件通知?

java - Android - 为什么 Activity 在点击通知后打开?

ios - 我可以通过编程方式将 iPhone 设置为边缘网络吗?

ios - 以百分比形式查看维度

ios - Pinterest 风格布局 iOS Storyboard