2
votes

I have a react native app where I am using RNCAsyncStorage npm module for simple data persistence. How ever, my iOS version of the app ships with a share extension, and this share extensions needs to access one of my stored data strings.

So in short, I want to use RNCAynscStorage from swift native code to get the data which was stored from my react native code.

Before even trying to implement the module I asked RNC and they replied:

This is possible but requires some knowledge of how native modules work in general. I'm not sure how future-proof this is with regards to the upcoming refactoring work in React Native but this is how you could access it today:

// First, get the RNCAsyncStorage instance from the bridge
RNCAsyncStorage *asyncStorage = [bridge moduleForClass:[RNCAsyncStorage class]];

// Then call the getter:
[asyncStorage multiGet:@[@"count"]] callback:^(NSArray *response){
    NSObject *count = response[0];
    if ([count isKindOfClass:[NSError class]]) {
        // Failed to get count
        return;
    }

    // Use count here
}];

However, My extension is written in swift, and I am not sure how this would look when converted to swift.

First I followed a guide to make the react bridge module accessible in swift code, this is how my brinding looks: enter image description here

I also tried to convert the objecttive-C example to seift, but this is not working as you can see. enter image description here I am not sure if this is because the module is not exposed/imported correctly, or simply if the swift conversion is wrong. but both bridge and RNCAsyncStorage is unresolved.

1

1 Answers

3
votes

As I understand, you want to access AsyncStorage in extension(today or share)

I am considering that your extension code will be in swift.

Step 1: First, you need to create a bridging header in the extension itself, not in the main project.

Step 2: Declare all the libraries you want to access in Share Extension Target In Link Binary With Libraries (Under Build Phase), especially (libRNCAsyncStorage.a)

Step 3: In bridge header file(.h) you have to import RNCAsyncStorage.h

Step 4: Now, access AsyncStore anywhere in the extension.