react-native-share 10.2.1 → 11.0.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/ios/RNShare.h +1 -1
- package/ios/RNShare.mm +1 -1
- package/ios/WhatsAppShare.m +153 -71
- package/package.json +1 -1
package/ios/RNShare.h
CHANGED
package/ios/RNShare.mm
CHANGED
package/ios/WhatsAppShare.m
CHANGED
|
@@ -8,87 +8,169 @@
|
|
|
8
8
|
|
|
9
9
|
#import "WhatsAppShare.h"
|
|
10
10
|
|
|
11
|
+
typedef NS_ENUM(NSInteger, MessageType) {
|
|
12
|
+
MessageTypeImage,
|
|
13
|
+
MessageTypeVideo,
|
|
14
|
+
MessageTypeText
|
|
15
|
+
};
|
|
16
|
+
|
|
11
17
|
@implementation WhatsAppShare
|
|
12
18
|
static UIDocumentInteractionController *documentInteractionController;
|
|
13
19
|
RCT_EXPORT_MODULE();
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
|
|
20
|
+
|
|
21
|
+
|
|
22
|
+
- (void) shareSingle:(NSDictionary *)options
|
|
23
|
+
reject:(RCTPromiseRejectBlock)reject
|
|
24
|
+
resolve:(RCTPromiseResolveBlock)resolve {
|
|
17
25
|
|
|
18
26
|
NSLog(@"Try open view");
|
|
19
27
|
|
|
20
|
-
if
|
|
21
|
-
|
|
22
|
-
|
|
23
|
-
|
|
28
|
+
if(![self isAbleToSendMessage: options]){
|
|
29
|
+
return;
|
|
30
|
+
}
|
|
31
|
+
|
|
32
|
+
if(![self isWhatsAppAvailable]) {
|
|
33
|
+
[self handleError:@"Not Installed" code:1 rejectFn:reject];
|
|
34
|
+
return;
|
|
35
|
+
}
|
|
36
|
+
|
|
37
|
+
MessageType messageType = [self getMessageType: options[@"url"]];
|
|
38
|
+
|
|
39
|
+
switch(messageType) {
|
|
40
|
+
case MessageTypeImage:
|
|
41
|
+
[self tryToSendImage: options resolve:resolve reject:reject];
|
|
42
|
+
break;
|
|
24
43
|
|
|
25
|
-
|
|
26
|
-
|
|
27
|
-
|
|
28
|
-
// Cannot open whatsapp
|
|
29
|
-
NSString *stringURL = @"https://itunes.apple.com/app/whatsapp-messenger/id310633997";
|
|
30
|
-
NSURL *url = [NSURL URLWithString:stringURL];
|
|
31
|
-
[[UIApplication sharedApplication] openURL:url];
|
|
32
|
-
|
|
33
|
-
NSString *errorMessage = @"Not installed";
|
|
34
|
-
NSDictionary *userInfo = @{NSLocalizedFailureReasonErrorKey: NSLocalizedString(errorMessage, nil)};
|
|
35
|
-
NSError *error = [NSError errorWithDomain:@"com.rnshare" code:1 userInfo:userInfo];
|
|
36
|
-
|
|
37
|
-
NSLog(@"%@", errorMessage);
|
|
38
|
-
return reject(@"com.rnshare",@"Not installed",error);
|
|
39
|
-
}
|
|
44
|
+
case MessageTypeVideo:
|
|
45
|
+
[self tryToSendVideo: options resolve:resolve reject:reject];
|
|
46
|
+
break;
|
|
40
47
|
|
|
41
|
-
|
|
42
|
-
|
|
43
|
-
|
|
44
|
-
|
|
45
|
-
|
|
46
|
-
|
|
47
|
-
|
|
48
|
-
|
|
49
|
-
|
|
50
|
-
|
|
51
|
-
|
|
52
|
-
|
|
53
|
-
|
|
54
|
-
|
|
55
|
-
|
|
56
|
-
|
|
57
|
-
|
|
58
|
-
|
|
59
|
-
|
|
60
|
-
NSError *error = [NSError errorWithDomain:@"com.rnshare" code:2 userInfo:@{ NSLocalizedDescriptionKey:@"Something went wrong"}];
|
|
61
|
-
return reject(@"com.rnshare",@"Something went wrong",error);
|
|
62
|
-
}
|
|
63
|
-
image = [UIImage imageWithData: data];
|
|
64
|
-
NSString * documentsPath = [NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES) objectAtIndex:0];
|
|
65
|
-
NSString * filePath = [documentsPath stringByAppendingPathComponent:@"/whatsAppTmp.wai"];
|
|
66
|
-
|
|
67
|
-
[UIImageJPEGRepresentation(image, 1.0) writeToFile:filePath atomically:YES];
|
|
68
|
-
|
|
69
|
-
documentInteractionController = [UIDocumentInteractionController interactionControllerWithURL:[NSURL fileURLWithPath:filePath]];
|
|
70
|
-
documentInteractionController.UTI = @"net.whatsapp.image";
|
|
71
|
-
documentInteractionController.delegate = self;
|
|
72
|
-
|
|
73
|
-
[documentInteractionController presentOpenInMenuFromRect:CGRectZero inView:[[[[[UIApplication sharedApplication] delegate] window] rootViewController] view] animated:YES];
|
|
74
|
-
resolve(@[@true, @""]);
|
|
75
|
-
}
|
|
76
|
-
} else {
|
|
77
|
-
NSError *error = [NSError errorWithDomain:@"com.rnshare" code:3 userInfo:@{ NSLocalizedDescriptionKey:@"Something went wrong"}];
|
|
78
|
-
return reject(@"com.rnshare",@"Something went wrong",error);
|
|
79
|
-
}
|
|
80
|
-
} else {
|
|
81
|
-
text = (NSString*)CFBridgingRelease(CFURLCreateStringByAddingPercentEscapes(NULL,(CFStringRef) text, NULL,CFSTR("!*'();:@&=+$,/?%#[]"),kCFStringEncodingUTF8));
|
|
82
|
-
|
|
83
|
-
NSString * urlWhats = whatsAppNumber ? [NSString stringWithFormat:@"whatsapp://send?phone=%@&text=%@", whatsAppNumber, text] : [NSString stringWithFormat:@"whatsapp://send?text=%@", text];
|
|
84
|
-
NSURL * whatsappURL = [NSURL URLWithString:urlWhats];
|
|
85
|
-
|
|
86
|
-
if ([[UIApplication sharedApplication] canOpenURL: whatsappURL]) {
|
|
87
|
-
[[UIApplication sharedApplication] openURL: whatsappURL];
|
|
88
|
-
resolve(@[@true, @""]);
|
|
89
|
-
}
|
|
48
|
+
default:
|
|
49
|
+
[self tryToSendText: options resolve:resolve reject:reject];
|
|
50
|
+
}
|
|
51
|
+
|
|
52
|
+
}
|
|
53
|
+
|
|
54
|
+
-(void)tryToSendImage:(NSDictionary *)options
|
|
55
|
+
resolve:(RCTPromiseResolveBlock)resolve
|
|
56
|
+
reject:(RCTPromiseRejectBlock)reject {
|
|
57
|
+
UIImage *image;
|
|
58
|
+
NSURL *imageURL = [RCTConvert NSURL:options[@"url"]];
|
|
59
|
+
if(imageURL){
|
|
60
|
+
if (imageURL.fileURL || [imageURL.scheme.lowercaseString isEqualToString:@"data"]) {
|
|
61
|
+
NSError *error;
|
|
62
|
+
NSData *data = [NSData dataWithContentsOfURL:imageURL
|
|
63
|
+
options:(NSDataReadingOptions)0
|
|
64
|
+
error:&error];
|
|
65
|
+
if (!data) {
|
|
66
|
+
return [self handleError:@"Something went wrong" code:2 rejectFn:reject];
|
|
90
67
|
}
|
|
68
|
+
|
|
69
|
+
image = [UIImage imageWithData: data];
|
|
70
|
+
NSString * documentsPath = [NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES) objectAtIndex:0];
|
|
71
|
+
NSString * filePath = [documentsPath stringByAppendingPathComponent:@"/whatsAppTmp.wai"];
|
|
72
|
+
|
|
73
|
+
[UIImageJPEGRepresentation(image, 1.0) writeToFile:filePath atomically:YES];
|
|
74
|
+
|
|
75
|
+
[self shareMedia:filePath documentUTI:@"net.whatsapp.image"];
|
|
76
|
+
|
|
77
|
+
resolve(@[@true, @""]);
|
|
78
|
+
}
|
|
79
|
+
|
|
80
|
+
}else {
|
|
81
|
+
[self handleError:@"Something went wrong" code:3 rejectFn:reject];
|
|
82
|
+
}
|
|
83
|
+
|
|
84
|
+
}
|
|
85
|
+
|
|
86
|
+
-(void)tryToSendVideo:(NSDictionary *)options
|
|
87
|
+
resolve:(RCTPromiseResolveBlock)resolve
|
|
88
|
+
reject:(RCTPromiseRejectBlock)reject {
|
|
89
|
+
|
|
90
|
+
NSLog(@"Sending whatsapp movie");
|
|
91
|
+
[self shareMedia:options[@"url"] documentUTI:@"net.whatsapp.movie"];
|
|
92
|
+
NSLog(@"Done whatsapp movie");
|
|
93
|
+
resolve(@[@true, @""]);
|
|
94
|
+
}
|
|
95
|
+
|
|
96
|
+
-(void)tryToSendText:(NSDictionary *)options
|
|
97
|
+
resolve:(RCTPromiseResolveBlock)resolve
|
|
98
|
+
reject:(RCTPromiseRejectBlock)reject {
|
|
99
|
+
|
|
100
|
+
NSString *text = [RCTConvert NSString:options[@"message"]];
|
|
101
|
+
text = [text stringByAppendingString: [@" " stringByAppendingString: options[@"url"]] ];
|
|
102
|
+
NSString *whatsAppNumber = [RCTConvert NSString:options[@"whatsAppNumber"]];
|
|
103
|
+
|
|
104
|
+
text = (NSString*)CFBridgingRelease(CFURLCreateStringByAddingPercentEscapes(NULL,(CFStringRef) text, NULL,CFSTR("!*'();:@&=+$,/?%#[]"),kCFStringEncodingUTF8));
|
|
105
|
+
|
|
106
|
+
NSString * urlWhats = whatsAppNumber ? [NSString stringWithFormat:@"whatsapp://send?phone=%@&text=%@", whatsAppNumber, text] : [NSString stringWithFormat:@"whatsapp://send?text=%@", text];
|
|
107
|
+
NSURL * whatsappURL = [NSURL URLWithString:urlWhats];
|
|
108
|
+
|
|
109
|
+
if ([[UIApplication sharedApplication] canOpenURL: whatsappURL]) {
|
|
110
|
+
[[UIApplication sharedApplication] openURL: whatsappURL];
|
|
111
|
+
resolve(@[@true, @""]);
|
|
112
|
+
}
|
|
113
|
+
}
|
|
114
|
+
|
|
115
|
+
|
|
116
|
+
-(MessageType)getMessageType: (NSString *)url {
|
|
117
|
+
NSArray *imageExtensions = @[@"png", @"jpeg",@"jpg",@"gif"];
|
|
118
|
+
if([self isMediaType:url mediaExtensions: imageExtensions]){
|
|
119
|
+
return MessageTypeImage;
|
|
120
|
+
}
|
|
121
|
+
|
|
122
|
+
NSArray *videoExtensions = @[@".wam", @".mp4"];
|
|
123
|
+
if([self isMediaType:url mediaExtensions:videoExtensions]){
|
|
124
|
+
return MessageTypeVideo;
|
|
125
|
+
}
|
|
126
|
+
|
|
127
|
+
return MessageTypeText;
|
|
128
|
+
}
|
|
129
|
+
|
|
130
|
+
-(BOOL)isMediaType:(NSString *)url mediaExtensions:(NSArray *)mediaExtensions {
|
|
131
|
+
for(NSString *extension in mediaExtensions){
|
|
132
|
+
if([url rangeOfString:extension].location != NSNotFound){
|
|
133
|
+
return TRUE;
|
|
91
134
|
}
|
|
135
|
+
}
|
|
136
|
+
return FALSE;
|
|
137
|
+
}
|
|
138
|
+
|
|
139
|
+
-(BOOL)isWhatsAppAvailable {
|
|
140
|
+
if([[UIApplication sharedApplication] canOpenURL: [NSURL URLWithString:@"whatsapp://app"]]) {
|
|
141
|
+
NSLog(@"WhastApp installed");
|
|
142
|
+
return true;
|
|
143
|
+
}else {
|
|
144
|
+
// Cannot open whatsapp
|
|
145
|
+
NSString *appStoreStringURL = @"https://itunes.apple.com/app/whatsapp-messenger/id310633997";
|
|
146
|
+
NSURL *appStoreURL = [NSURL URLWithString:appStoreStringURL];
|
|
147
|
+
[[UIApplication sharedApplication] openURL:appStoreURL];
|
|
148
|
+
return false;
|
|
149
|
+
}
|
|
150
|
+
}
|
|
151
|
+
|
|
152
|
+
-(void)handleError:(NSString *)errorMessage code:(NSInteger)code rejectFn:(RCTPromiseRejectBlock)rejectFn {
|
|
153
|
+
NSDictionary *userInfo = @{NSLocalizedFailureReasonErrorKey: NSLocalizedString(errorMessage, nil)};
|
|
154
|
+
NSError *error = [NSError errorWithDomain:@"com.rnshare" code:code userInfo:userInfo];
|
|
155
|
+
|
|
156
|
+
NSLog(@"%@", errorMessage);
|
|
157
|
+
return rejectFn(@"com.rnshare",errorMessage,error);
|
|
158
|
+
}
|
|
159
|
+
|
|
160
|
+
|
|
161
|
+
-(BOOL)isAbleToSendMessage:(NSDictionary *) options {
|
|
162
|
+
if([options objectForKey:@"message"] && [options objectForKey:@"message"] != [NSNull null]){
|
|
163
|
+
return TRUE;
|
|
164
|
+
}
|
|
165
|
+
return FALSE;
|
|
166
|
+
}
|
|
167
|
+
|
|
168
|
+
-(void)shareMedia:(NSString *)stringURL documentUTI:(NSString *)documentUTI {
|
|
169
|
+
NSURL *filePath = [NSURL fileURLWithPath:stringURL];
|
|
170
|
+
documentInteractionController = [UIDocumentInteractionController interactionControllerWithURL:filePath];
|
|
171
|
+
documentInteractionController.UTI = documentUTI;
|
|
172
|
+
documentInteractionController.delegate = self;
|
|
173
|
+
[documentInteractionController presentOpenInMenuFromRect:CGRectZero inView:[[[[[UIApplication sharedApplication] delegate] window] rootViewController] view] animated:YES];
|
|
92
174
|
}
|
|
93
175
|
|
|
94
176
|
@end
|
package/package.json
CHANGED