Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
14 changes: 14 additions & 0 deletions ios/ReactNativeShareExtension.h
Original file line number Diff line number Diff line change
Expand Up @@ -2,5 +2,19 @@
#import "React/RCTBridgeModule.h"

@interface ReactNativeShareExtension : UIViewController<RCTBridgeModule>

/**
* @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
29 changes: 27 additions & 2 deletions ios/ReactNativeShareExtension.m
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -18,6 +22,10 @@ - (UIView*) shareView {
return nil;
}

- (UIView*) shareViewWithRCTBridge:(RCTBridge*)sharedBridge {
return nil;
}

RCT_EXPORT_MODULE();

- (void)viewDidLoad {
Expand All @@ -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];
}
Expand All @@ -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) {
Expand Down