Skip to content
This repository has been archived by the owner on Feb 24, 2020. It is now read-only.

Added Promises support #2

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
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
28 changes: 17 additions & 11 deletions ios/RNInstagramStoryShare/RNInstagramStoryShare.m
Original file line number Diff line number Diff line change
Expand Up @@ -10,16 +10,22 @@
#endif

@implementation RNInstagramStoryShare

RCT_EXPORT_MODULE();

- (dispatch_queue_t)methodQueue
{
return dispatch_get_main_queue();
}

RCT_EXPORT_MODULE();
+ (BOOL)requiresMainQueueSetup
{
return YES;
}

RCT_EXPORT_METHOD(share:(NSDictionary *)options
failureCallback:(RCTResponseErrorBlock)failureCallback
successCallback:(RCTResponseSenderBlock)successCallback) {
resolve:(RCTPromiseResolveBlock)resolve
reject:(RCTPromiseRejectBlock)reject) {
NSString *deeplinkingUrl = [RCTConvert NSString:options[@"deeplinkingUrl"]];
NSURL *backgroundImageUrl = [RCTConvert NSURL:options[@"backgroundImage"]];

Expand All @@ -35,38 +41,38 @@ - (dispatch_queue_t)methodQueue
error:&error];

if (!data) {
failureCallback(error);
reject(RCTErrorUnspecified, errorMessage, error);
return;
}

/* deeplink to instagram */
[self backgroundImage:UIImagePNGRepresentation([UIImage imageWithData:data])
attributionURL: deeplinkingUrl
failureCallback:failureCallback
successCallback:successCallback];
resolve:resolve
reject:reject];

} else {
/* handle non base64 images */
NSString *errorMessage = @"No base64 image";
NSDictionary *userInfo = @{NSLocalizedFailureReasonErrorKey: NSLocalizedString(errorMessage, nil)};
NSError *error = [NSError errorWithDomain:@"com.rninstagramstoryshare" code:1 userInfo:userInfo];

failureCallback(error);
reject(RCTErrorUnspecified, errorMessage, error);
}
} else {
/* handle non existing assets */
NSString *errorMessage = @"Invalid parameter";
NSDictionary *userInfo = @{NSLocalizedFailureReasonErrorKey: NSLocalizedString(errorMessage, nil)};
NSError *error = [NSError errorWithDomain:@"com.rninstagramstoryshare" code:1 userInfo:userInfo];

failureCallback(error);
reject(RCTErrorUnspecified, errorMessage, error);
}
}

- (void)backgroundImage:(NSData *)backgroundImage
attributionURL:(NSString *)attributionURL
failureCallback:(RCTResponseErrorBlock)failureCallback
successCallback:(RCTResponseSenderBlock)successCallback
resolve:(RCTPromiseResolveBlock)resolve
reject:(RCTPromiseRejectBlock)reject
{

// Verify app can open custom URL scheme, open if able
Expand Down Expand Up @@ -94,7 +100,7 @@ - (void)backgroundImage:(NSData *)backgroundImage
NSDictionary *userInfo = @{NSLocalizedFailureReasonErrorKey: NSLocalizedString(errorMessage, nil)};
NSError *error = [NSError errorWithDomain:@"com.rninstagramstoryshare" code:1 userInfo:userInfo];

failureCallback(error);
reject(RCTErrorUnspecified, errorMessage, error);
}
}

Expand Down