Skip to content

Commit

Permalink
feat(ios): support added for instagram direct message (#1255)
Browse files Browse the repository at this point in the history
* feat(ios): support added for instagram direct message

* feat(ios): example updated for instagram direct message

* fix(spaces): extra spaces removed

Co-authored-by: Arsal Raza <arsal.raza@purevpn.com>
Co-authored-by: Mateus Andrade <15278828+MateusAndrade@users.noreply.github.com>
  • Loading branch information
3 people committed Aug 7, 2022
1 parent d7102da commit 146126b
Show file tree
Hide file tree
Showing 3 changed files with 38 additions and 11 deletions.
18 changes: 18 additions & 0 deletions example/App.js
Expand Up @@ -163,6 +163,21 @@ const App = () => {
}
};

const shareToInstagramDirect = async () => {
const shareOptions = {
message: encodeURI('Checkout the great search engine: https://google.com'),
social: Share.Social.INSTAGRAM,
};

try {
const ShareResponse = await Share.shareSingle(shareOptions);
setResult(JSON.stringify(ShareResponse, null, 2));
} catch (error) {
console.log('Error =>', error);
setResult('error: '.concat(getErrorString(error)));
}
};

const shareToInstagramStory = async () => {
const shareOptions = {
title: 'Share image to instastory',
Expand Down Expand Up @@ -299,6 +314,9 @@ const App = () => {
<View style={styles.button}>
<Button onPress={shareToInstagramStory} title="Share to IG Story" />
</View>
<View style={styles.button}>
<Button onPress={shareToInstagramDirect} title="Share to IG Direct" />
</View>
<View style={styles.button}>
<Button onPress={shareToFacebookStory} title="Share to FB Story" />
</View>
Expand Down
29 changes: 19 additions & 10 deletions ios/InstagramShare.m
Expand Up @@ -16,17 +16,26 @@ - (void)shareSingle:(NSDictionary *)options
successCallback:(RCTResponseSenderBlock)successCallback {

NSLog(@"Try open view");

NSURL * fileURL = [NSURL URLWithString: options[@"url"]];
AVURLAsset* videoAsset = [AVURLAsset URLAssetWithURL:fileURL options:nil];
CMTime videoDuration = videoAsset.duration;
float videoDurationSeconds = CMTimeGetSeconds(videoDuration);

NSLog(@"Video duration: %f seconds for file %@", videoDurationSeconds, videoAsset.URL.absoluteString);


NSURL * shareURL;
// Instagram doesn't allow sharing videos longer than 60 seconds on iOS anymore. (next button is not responding, trim is unavailable)
if (videoDurationSeconds <= 60.0f) {
float videoDurationSeconds = 0.0f;
NSString* url = options[@"url"];
if (url) {
NSURL * fileURL = [NSURL URLWithString: options[@"url"]];
AVURLAsset* videoAsset = [AVURLAsset URLAssetWithURL:fileURL options:nil];
CMTime videoDuration = videoAsset.duration;
float videoDurationSeconds = CMTimeGetSeconds(videoDuration);

NSLog(@"Video duration: %f seconds for file %@", videoDurationSeconds, videoAsset.URL.absoluteString);
} else {
//this will send message directly to instagram DM with plain text
shareURL = [NSURL URLWithString:[NSString stringWithFormat:@"instagram://sharesheet?text=%@", options[@"message"]]];
}

if (shareURL) {
NSLog(@"url is already available, no need to do anything");
} else if (videoDurationSeconds <= 60.0f) {
// Instagram doesn't allow sharing videos longer than 60 seconds on iOS anymore. (next button is not responding, trim is unavailable)
NSString *phIdentifier= [options[@"url"] stringByReplacingOccurrencesOfString:@"ph://" withString:@""];
NSString * urlString = [NSString stringWithFormat:@"instagram://library?LocalIdentifier=%@", phIdentifier];
shareURL = [NSURL URLWithString:urlString];
Expand Down
2 changes: 1 addition & 1 deletion ios/RNShare.m
Expand Up @@ -63,7 +63,7 @@ - (CGRect)sourceRectInView:(UIView *)sourceView

- (BOOL)isImageMimeType:(NSString *)data {
NSRange range = [data rangeOfString:@"data:image" options:NSCaseInsensitiveSearch];
if (range.location != NSNotFound) {
if (data && range.location != NSNotFound) {
return true;
} else {
return false;
Expand Down

0 comments on commit 146126b

Please sign in to comment.