Skip to content

Instantly share code, notes, and snippets.

@tolmekian1453
Last active August 17, 2019 20:52
Show Gist options
  • Save tolmekian1453/6785e85f4dd88f950fcfc20d95d44dd3 to your computer and use it in GitHub Desktop.
Save tolmekian1453/6785e85f4dd88f950fcfc20d95d44dd3 to your computer and use it in GitHub Desktop.
Firebase React Native Share Extension Credentials Syncer
// see https://github.com/firebase/firebase-ios-sdk/issues/2179
//
// this is a hack workaround that periodically copies the Firebase credentials
// between your main app and your share extension,
// thus allowing the extension to authenticate using the same creds
//
// make sure you've created an app group beforehand so they can share that storage
#import <Foundation/Foundation.h>
#import "FirebaseAuthSync.h"
#define RCT_DIR (@"RCTAsyncLocalStorage_V1") // as of 8/17/2019. idk if this'll change. Hacky.
#define APP_GROUP (@"YOUR.GROUP.ID.HERE") // TODO change to e.g. (@"group.com.foo.bar")
@interface FirebaseAuthSync : NSObject
+ (void) sync: (BOOL) mainApp;
+ (void) timer: (BOOL) mainApp;
@end
@implementation FirebaseAuthSync
+ (void) sync: (BOOL) mainApp {
NSURL* url = [[NSFileManager defaultManager] containerURLForSecurityApplicationGroupIdentifier: APP_GROUP];
NSString* sharedDir = [NSString stringWithFormat:@"%@/%@", [url path], RCT_DIR];
NSString* docsDir = [NSString stringWithFormat:@"%@/%@", NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES)[0], RCT_DIR];
NSString* src = NULL;
NSString* dst = NULL;
if (mainApp) {
src = docsDir;
dst = sharedDir;
} else {
src = sharedDir;
dst = docsDir;
}
NSError* error;
[[NSFileManager defaultManager] removeItemAtPath:dst error:&error];
NSLog(@"Deleted item %@, error (ok if null): %@", dst, error);
[[NSFileManager defaultManager] copyItemAtURL: [NSURL fileURLWithPath:src] toURL: [NSURL fileURLWithPath:dst] error: &error];
NSLog(@"Copying from %@ to %@, error (ok if null): %@", src, dst, error);
}
+ (void) timer: (BOOL) mainApp {
[FirebaseAuthSync sync: mainApp];
dispatch_after(dispatch_time(DISPATCH_TIME_NOW, 60 * NSEC_PER_SEC), dispatch_get_main_queue(), ^{
NSLog(@"Timer for FirebaseAuthSync firing");
[FirebaseAuthSync timer: mainApp];
});
}
@end
@tolmekian1453
Copy link
Author

in your AppDelegate.m:

- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{
  [FirebaseAuthSync timer: true];

in your share extension's .m, if you're using react-native-share-extension's instructions:

- (UIView*) shareView {
  [FirebaseAuthSync sync: false];

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment