将本机iOS应用程序的数据迁移到React Native应用程序的异步存储 [英] Migrating native iOS app's data to React Native app's asyncstorage

查看:73
本文介绍了将本机iOS应用程序的数据迁移到React Native应用程序的异步存储的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

从本地iOS(Objective-C)开发迁移以响应本地开发时,当用户从应用商店中更新应用时,我们如何保持现有用户登录"并为用户提供无缝过渡.

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.

要做到这一点...

  1. 我们需要从持久性存储(通常为NSDictionary)中读取数据(即用户名,电子邮件,访问令牌)
  2. 将其存储在react本机框架提供的asyncStorage中

问题是:

  1. 是否有任何通过本机反应的辅助方法?
  2. 还有其他图书馆可以这样做吗?
  3. 如果前两个问题的答案为否",那么在Objective-C中该如何做?

推荐答案

我们最近在其中一个应用中使用了相同的用例,并决定执行以下操作:

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

像通常一样从NSUserDictionary读取(获取所有元素)并将它们传递给RCTRootView

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
                                                      moduleName:@"XYZ"
                                               initialProperties:userSettings
                                                 launchOptions:launchOptions];

假设您的userSettings词典包含例如accessToken属性,您应该能够从根React组件(传递给AppRegistry.registerComponent的组件)内部访问this.props.accessToken.

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).

接下来,您可以检查值是否已经持久保存到AsyncStorage(或者是否应该使用任何其他的userSettings副本),如果不是,则保存当前值.

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.

另一种替代方法是使用React Native中可用的Settings模块,该模块允许您访问NSUserDefaults.它具有.get().set()方法,因此从技术上讲应该可以使用它.

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.

请注意,直到您从Xcode内部重新运行应用程序之后,通过initialProperties传递的userSettings才会更新.

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

这篇关于将本机iOS应用程序的数据迁移到React Native应用程序的异步存储的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆