adesh shah adesh shah - 1 year ago 102
iOS Question

Migrating native iOS app's data to React Native app's asyncstorage

While migrating from native iOS (Objective-C) development to react native development, how do we keep our existing user 'signed in' and make the transition seamless for the users when they update the app from the app store.

For this to happen...

  1. We need to read data (i.e. Username, Email, Accesstoken) from the persistent storage (Usually NSDictionary)

  2. Store it in the asyncStorage provided by the react native framework

The question is:

  1. Is there any helper method by react native to do so?

  2. Is there any other library to do so?

  3. If the answer is "no" for the first 2 questions, how to do it in Objective-C?

Answer Source

We have recently had the same use-case in one of our apps and decided to do the following:

Read from NSUserDictionary just like normally (fetch all the elements) and pass them to RCTRootView

NSDictionary *userSettings = @{}; // fetch them here
RCTRootView *rootView = [[RCTRootView alloc] initWithBundleURL:jsCodeLocation

Assuming your userSettings dictionary contains e.g. accessToken property, you should be able to just access this.props.accessToken from within your root React component (the one you pass to AppRegistry.registerComponent).

Next, you can either check if values have been already persisted to AsyncStorage (or is there any other copy of userSettings that you should use) and if not - persist the current values.

Another alternative is to use Settings module that is available in React Native that allows you to access NSUserDefaults. It has .get() and .set() methods, so technically it should be possible to use it.

Note that userSettings passed through initialProperties will not update until you re-run the app from within the Xcode.

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download