Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

React native - How to implement iOS Settings

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.

like image 409
Chris Sheffield Avatar asked Feb 16 '17 18:02

Chris Sheffield


People also ask

Can I build iOS app using React Native?

React Native enables you to write Android and iOS apps using JavaScript. It uses React's virtual DOM concept to manipulate native platform UI components, providing a familiar experience to users. There are two ways to build iOS apps: React Native CLI: More complex but gives you more control over your app.

How to open Settings app in React Native?

then you can check for the permission and if not provided/restricted, you can direct user to settings app to enable the same. Opening a settings app in React Native is so simple, we can open it directly in React Native using Linking. Linking API provides an openSettings () function which will redirect to Settings App.

How do I run react-native apps on iOS devices?

npx react-native run-ios is one way to run your app. You can also run it directly from within Xcode. If you can't get this to work, see the Troubleshooting page. The above command will automatically run your app on the iOS Simulator by default. If you want to run the app on an actual physical iOS device, please follow the instructions here.

How do I switch between node and React Native?

If you want to be able to switch between different Node versions, you might want to install Node via nvm-windows, a Node version manager for Windows. React Native also requires Java SE Development Kit (JDK), which can be installed using Chocolatey as well. If you have already installed Node on your system, make sure it is Node 12 or newer.

Which react native versions are supported in Expo client apps?

The Expo client app usually gains support for a given React Native version about a week after the React Native version is released as stable. You can check this document to find out what versions are supported.


2 Answers

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.

like image 109
Manav Avatar answered Oct 27 '22 19:10

Manav


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 also do not suggest since that t letting the user 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.

like image 32
joshfindit Avatar answered Oct 27 '22 19:10

joshfindit