简体   繁体   中英

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. I'd like the share extension to have access to the token set in the React Native app. 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.

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 . I'm basically new to Swift and Xcode so I have no idea if what I am doing should work. 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.

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. Thanks to kevinresol , who developed the library, for the solution.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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