23

Wondering if someone could help me with this, or at least point me in the right direction.

I've been searching for documentation on how to get/set settings in a React Native iOS app so that those settings appear in the iOS Settings app listed under my app. I see that there is a Settings API, but it appears that the documentation is not complete. The function definitions are listed there, but that's it. No examples or anything.

Can anyone provide me with a simple example, or point me to a tutorial or something that will help me get going? I'm assuming I import Settings from react-native, just like I would do for other APIs, but beyond that I'm not sure where to go.

Thanks.

Chris Sheffield
  • 231
  • 2
  • 4
  • 1
    Did you ever figure this out? I'm looking to implement the same thing right now and the documentation is very limited. I wonder if the only way is through a RN-Native bridge. – Fernando May 02 '17 at 19:52
  • 10
    Yes, I did eventually figure it out. The first step is to add a Settings bundle to your Xcode project. This is fairly easy to do. Simply do some searching in Apple's developer docs to find the steps. Then in your React Native code you can retrieve settings with something like var value = Settings.get('preference_name_here'). Make sure to import the Settings api, of course. – Chris Sheffield May 03 '17 at 21:30
  • 1
    @ChrisSheffield you should post your solution as an answer to your own question – rolling_codes Jun 20 '19 at 20:26

3 Answers3

1

As stated in React Native documentation :

Settings serves as a wrapper for NSUserDefaults, a persistent key-value store available only on iOS.

If you want to add iOS Settings bundle to your app you can use this.

Manav
  • 2,284
  • 1
  • 14
  • 27
1

As per Chris Sheffield's comment, here is what I have succeeded with so far:

  1. Add a Settings.bundle to your Xcode project
    • Highlight the project > File > New > File
    • Choose "Settings Bundle" > Next
    • I just left the default name: Settings.bundle
  2. Open the Root.plist file inside of the bundle enter image description here
  3. Make changes based on Apple's documentation (version I'm referencing is archived here: https://web.archive.org/...)
    • The important value to keep track of is the item's Identifier
  4. Save, compile, and install the app
  5. You can now use Settings.get('<identifier>') like either of these:
    • const varName = Settings.get('var_name')
    • const [ varName ] = useState(Settings.get('var_name'));

Notes

I suggest using some method of watching for changes so that your app updates when the user changes settings while it's running, but these are the only parts required.

I do not suggest letting the user also change those specific settings in-app since that goes against the principle of Single Source of Truth, but it's your app, you do what's best for you and your users.

joshfindit
  • 611
  • 6
  • 27
-2

Hope this plugin will help. react-native-permissions

export const _checkPermission = (permissionName) => {
    return Permissions.check(permissionName).then(response => {
        if (response === 'denied') {
            return false
        } else if (response === 'authorized') {
            return true
        } else if (response === 'restricted') {
            return false
        } else if (response === 'undetermined') {
            return false
        }
    })
}

also, you can use this for asking permission

_requestPermission = (permissionName) => {
    return Permissions.request(permissionName).then(response => {
        return response
    })
}


export const _alertForPermission = (permissionName) => {
   return _requestPermission(permissionName)
}
Rajan
  • 1,512
  • 2
  • 14
  • 18
  • Original question was about "How can I add toogle to MyApp settings in system-wide settings", but not "How can i get location permission for my app from the code?" – evasyuk Dec 15 '20 at 10:30