简体   繁体   English

在React Native应用和Swift Share Extension之间共享UserDefaults

[英]Share UserDefaults between React Native app and Swift Share Extension

I have a React Native App for which I am trying to build an iOS share extension, using Swift. 我有一个React Native App,我正在尝试使用Swift为它构建iOS共享扩展。 I'd like the share extension to have access to the token set in the React Native app. 我希望共享扩展可以访问React Native应用程序中设置的令牌。 I have set up an app group, which contains the app and the share extension. 我已经建立了一个应用程序组,其中包含应用程序和共享扩展名。

In the app, I am using react-native-default-preference to set UserDefaults. 在应用程序中,我正在使用react-native-default-preference设置UserDefaults。

import DefaultPreference from 'react-native-default-preference'

DefaultPreference.set('authToken', token)

In my share extension view controller, I have 在我的共享扩展视图控制器中,

if let userDefaults = UserDefaults(suiteName: "group.myGroup.myApp") {
    let value1 = userDefaults.string(forKey: "authToken")
    print("retrieved token value\(value1)")
}

This always prints out nil . 这总是打印nil I'm basically new to Swift and Xcode so I have no idea if what I am doing should work. 我基本上是Swift和Xcode的新手,所以我不知道自己在做什么。 I'm also not certain if I should have the app running in Xcode or the share extension running, so I keep switching back and forth between the two, but it doesn't work either way. 我也不确定是否应该在Xcode中运行应用程序或在共享扩展程序中运行,所以我一直在两者之间来回切换,但这两种方法均无效。

Is there something that I am missing, or can anyone offer an alternative solution? 是否有我缺少的东西,或者谁能提供替代解决方案? Many thanks in advance! 提前谢谢了!

I needed to call DefaultPreference.setName('group.myGroup.myApp') in my RN project. 我需要在我的RN项目中调用DefaultPreference.setName('group.myGroup.myApp') Thanks to kevinresol , who developed the library, for the solution. 感谢开发该库的kevinresol提供了解决方案。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM