Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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!

like image 831
Ian Lenehan Avatar asked Sep 10 '25 19:09

Ian Lenehan


1 Answers

I needed to call DefaultPreference.setName('group.myGroup.myApp') in my RN project. Thanks to kevinresol, who developed the library, for the solution.

like image 178
Ian Lenehan Avatar answered Sep 13 '25 08:09

Ian Lenehan