Created
November 4, 2022 11:35
-
-
Save akinkarayun/b0c44378ec27c6cf4b08950c98ca3472 to your computer and use it in GitHub Desktop.
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
// | |
// GenericShare.m | |
// RNShare | |
// | |
// Created by Diseño Uno BBCL on 23-07-16. | |
// Copyright © 2016 Facebook. All rights reserved. | |
// | |
#import "GenericShare.h" | |
@implementation GenericShare | |
RCT_EXPORT_MODULE(); | |
- (void)shareSingle:(NSDictionary *)options | |
failureCallback:(RCTResponseErrorBlock)failureCallback | |
successCallback:(RCTResponseSenderBlock)successCallback | |
serviceType:(NSString*)serviceType | |
inAppBaseUrl:(NSString *)inAppBaseUrl { | |
NSLog(@"Try open view"); | |
if([serviceType isEqualToString:@"com.apple.social.twitter"] && [[UIApplication sharedApplication] canOpenURL:[NSURL URLWithString:inAppBaseUrl]]) { | |
SLComposeViewController *composeController = [SLComposeViewController composeViewControllerForServiceType:serviceType]; | |
NSURL *URL = [RCTConvert NSURL:options[@"url"]]; | |
if (URL) { | |
if (URL.fileURL || [URL.scheme.lowercaseString isEqualToString:@"data"]) { | |
NSError *error; | |
NSData *data = [NSData dataWithContentsOfURL:URL | |
options:(NSDataReadingOptions)0 | |
error:&error]; | |
if (!data) { | |
failureCallback(error); | |
return; | |
} | |
UIImage *image = [UIImage imageWithData: data]; | |
[composeController addImage:image]; | |
} else { | |
[composeController addURL:URL]; | |
} | |
} | |
if ([options objectForKey:@"message"] && [options objectForKey:@"message"] != [NSNull null]) { | |
NSString *text = [RCTConvert NSString:options[@"message"]]; | |
[composeController setInitialText:text]; | |
} | |
UIViewController *ctrl = RCTPresentedViewController(); | |
[ctrl presentViewController:composeController animated:YES completion:Nil]; | |
successCallback(@[@true, @""]); | |
}else if([serviceType isEqualToString:@"com.apple.social.facebook"] && [[UIApplication sharedApplication] canOpenURL:[NSURL URLWithString:inAppBaseUrl]]) { | |
SLComposeViewController *composeController = [SLComposeViewController composeViewControllerForServiceType:serviceType]; | |
NSURL *URL = [RCTConvert NSURL:options[@"url"]]; | |
if (URL) { | |
if (URL.fileURL || [URL.scheme.lowercaseString isEqualToString:@"data"]) { | |
NSError *error; | |
NSData *data = [NSData dataWithContentsOfURL:URL | |
options:(NSDataReadingOptions)0 | |
error:&error]; | |
if (!data) { | |
failureCallback(error); | |
return; | |
} | |
UIImage *image = [UIImage imageWithData: data]; | |
[composeController addImage:image]; | |
} else { | |
[composeController addURL:URL]; | |
} | |
} | |
if ([options objectForKey:@"message"] && [options objectForKey:@"message"] != [NSNull null]) { | |
NSString *text = [RCTConvert NSString:options[@"message"]]; | |
[composeController setInitialText:text]; | |
} | |
UIViewController *ctrl = RCTPresentedViewController(); | |
[ctrl presentViewController:composeController animated:YES completion:Nil]; | |
successCallback(@[@true, @""]); | |
} | |
else { | |
NSString *errorMessage = @"Not installed"; | |
NSDictionary *userInfo = @{NSLocalizedFailureReasonErrorKey: NSLocalizedString(errorMessage, nil)}; | |
NSError *error = [NSError errorWithDomain:@"com.rnshare" code:1 userInfo:userInfo]; | |
NSLog(@"%@", errorMessage); | |
failureCallback(error); | |
NSString *escapedString = [options[@"message"] stringByAddingPercentEncodingWithAllowedCharacters:[NSCharacterSet URLHostAllowedCharacterSet]]; | |
if ([options[@"social"] isEqualToString:@"twitter"]) { | |
NSString *URL = [NSString stringWithFormat:@"https://itunes.apple.com/app/twitter/id333903271", escapedString, options[@"url"]]; | |
[self openScheme:URL]; | |
} | |
if ([options[@"social"] isEqualToString:@"facebook"]) { | |
NSString *URL = [NSString stringWithFormat:@"https://itunes.apple.com/app/facebook/id284882215", options[@"url"]]; | |
[self openScheme:URL]; | |
} | |
} | |
} | |
- (void)openScheme:(NSString *)scheme { | |
UIApplication *application = [UIApplication sharedApplication]; | |
NSURL *schemeURL = [NSURL URLWithString:scheme]; | |
if ([application respondsToSelector:@selector(openURL:options:completionHandler:)]) { | |
if (@available(iOS 10.0, *)) { | |
[application openURL:schemeURL options:@{} completionHandler:nil]; | |
} | |
NSLog(@"Open %@", schemeURL); | |
} | |
} | |
@end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment