From e1df8f805a031fc2d8b84783781467ff2622c0f4 Mon Sep 17 00:00:00 2001 From: Robert Scott Date: Fri, 8 Feb 2019 10:45:54 -0800 Subject: [PATCH] Reuse RCTBridge for the share view. Fixes #64 --- ios/ReactNativeShareExtension.h | 14 ++++++++++++++ ios/ReactNativeShareExtension.m | 29 +++++++++++++++++++++++++++-- 2 files changed, 41 insertions(+), 2 deletions(-) diff --git a/ios/ReactNativeShareExtension.h b/ios/ReactNativeShareExtension.h index c1b2559a..6e9bf65b 100644 --- a/ios/ReactNativeShareExtension.h +++ b/ios/ReactNativeShareExtension.h @@ -2,5 +2,19 @@ #import "React/RCTBridgeModule.h" @interface ReactNativeShareExtension : UIViewController + +/** + * @deprecated This method should not be used unless you are creating + * a shared bridge inside of shareView. + * @note Please use @code shareViewWithRCTBridge @endcode instead. + */ - (UIView*) shareView; + +/** + * Create a shareView using a common RCTBridge. The RCTBridge is reused + * for each launch of the share sheet. This allows the share sheet to + * free its resources in between launches. + */ +- (UIView*) shareViewWithRCTBridge:(RCTBridge*)sharedBridge; + @end diff --git a/ios/ReactNativeShareExtension.m b/ios/ReactNativeShareExtension.m index ddf1e692..2b478a2b 100644 --- a/ios/ReactNativeShareExtension.m +++ b/ios/ReactNativeShareExtension.m @@ -8,6 +8,10 @@ NSExtensionContext* extensionContext; +// Save a copy of the RCTBridge to reuse. Creating a new bridge +// each time this saves mempory. +RCTBridge *sharedBridge; + @implementation ReactNativeShareExtension { NSTimer *autoTimer; NSString* type; @@ -18,6 +22,10 @@ - (UIView*) shareView { return nil; } +- (UIView*) shareViewWithRCTBridge:(RCTBridge*)sharedBridge { + return nil; +} + RCT_EXPORT_MODULE(); - (void)viewDidLoad { @@ -27,7 +35,19 @@ - (void)viewDidLoad { //variable extensionContext. in this way, both exported method can touch extensionContext extensionContext = self.extensionContext; - UIView *rootView = [self shareView]; + if (sharedBridge == nil) { + sharedBridge = [[RCTBridge alloc] initWithBundleURL:jsCodeLocation + moduleProvider:nil + launchOptions:nil]; + } + + UIView *rootView = [self shareViewWithRCTBridge:sharedBridge]; + + if (rootView == nil) { + // Fallback to the previous version if shareViewWithRCTBridge isn't implemented. + rootView = [self shareView]; + } + if (rootView.backgroundColor == nil) { rootView.backgroundColor = [[UIColor alloc] initWithRed:1 green:1 blue:1 alpha:0.1]; } @@ -39,8 +59,13 @@ - (void)viewDidLoad { RCT_EXPORT_METHOD(close) { [extensionContext completeRequestReturningItems:nil completionHandler:nil]; -} + // Set the view to nil so it gets cleaned up. + self.view = nil; + + [sharedBridge invalidate]; + sharedBridge = nil; +} RCT_EXPORT_METHOD(openURL:(NSString *)url) {