Last active
August 17, 2019 20:52
-
-
Save tolmekian1453/6785e85f4dd88f950fcfc20d95d44dd3 to your computer and use it in GitHub Desktop.
Firebase React Native Share Extension Credentials Syncer
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// 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 |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
in your AppDelegate.m:
in your share extension's .m, if you're using react-native-share-extension's instructions: