forked from dosomegood/react-native-activity-view
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathActivityView.m
176 lines (143 loc) · 6.08 KB
/
ActivityView.m
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
#import "ActivityView.h"
#import <React/RCTLog.h>
#import <React/RCTBridge.h>
#import <React/RCTUIManager.h>
#import <React/RCTImageLoader.h>
#import <React/RCTUtils.h>
@implementation ActivityView
@synthesize bridge = _bridge;
- (dispatch_queue_t)methodQueue
{
return dispatch_get_main_queue();
}
RCT_EXPORT_MODULE()
// Map user passed array of strings to UIActivities
- (NSArray*)excludedActivitiesForKeys:(NSArray*)passedKeys {
NSDictionary *activities = @{
@"postToFacebook": UIActivityTypePostToFacebook,
@"postToTwitter": UIActivityTypePostToTwitter,
@"postToWeibo": UIActivityTypePostToWeibo,
@"message": UIActivityTypeMessage,
@"mail": UIActivityTypeMail,
@"print": UIActivityTypePrint,
@"copyToPasteboard": UIActivityTypeCopyToPasteboard,
@"assignToContact": UIActivityTypeAssignToContact,
@"saveToCameraRoll": UIActivityTypeSaveToCameraRoll,
@"addToReadingList": UIActivityTypeAddToReadingList,
@"postToFlickr": UIActivityTypePostToFlickr,
@"postToVimeo": UIActivityTypePostToVimeo,
@"postToTencentWeibo": UIActivityTypePostToTencentWeibo,
@"airDrop": UIActivityTypeAirDrop
};
NSMutableArray *excludedActivities = [NSMutableArray new];
[passedKeys enumerateObjectsUsingBlock:^(NSString *activityName, NSUInteger idx, BOOL *stop) {
NSString *activity = [activities objectForKey:activityName];
if (!activity) {
RCTLogWarn(@"[ActivityView] Unknown activity to exclude: %@. Expected one of: %@", activityName, [activities allKeys]);
return;
}
[excludedActivities addObject:activity];
}];
return excludedActivities;
}
RCT_EXPORT_METHOD(show:(NSDictionary *)args callback:(RCTResponseSenderBlock)callback)
{
NSString *imageUrl = args[@"imageUrl"];
NSString *image = args[@"image"];
NSString *imageBase64 = args[@"imageBase64"];
__block UIImage *shareImage;
if (image) {
shareImage = [UIImage imageNamed:image];
}
if (imageBase64) {
@try {
NSData *decodedImage = [[NSData alloc] initWithBase64EncodedString:imageBase64
options:NSDataBase64DecodingIgnoreUnknownCharacters];
shareImage = [UIImage imageWithData:decodedImage];
} @catch (NSException *exception) {
RCTLogWarn(@"[ActivityView] Could not decode image");
}
}
if (!imageUrl) {
return [self showWithOptions:args image:shareImage callback:callback];
}
RCTImageLoader *loader = (RCTImageLoader*)[self.bridge moduleForClass:[RCTImageLoader class]];
__weak ActivityView *weakSelf = self;
[loader loadImageWithURLRequest:[RCTConvert NSURLRequest:imageUrl] callback:^(NSError *error, id imageOrData) {
if (!error) {
if ([imageOrData isKindOfClass:[NSData class]]) {
shareImage = [UIImage imageWithData:imageOrData];
} else {
shareImage = imageOrData;
}
} else {
RCTLogWarn(@"[ActivityView] Could not fetch image.");
}
dispatch_async([weakSelf methodQueue], ^{
[weakSelf showWithOptions:args image:shareImage callback:callback];
});
}];
}
- (void) showWithOptions:(NSDictionary *)args image:(UIImage *)image callback:(RCTResponseSenderBlock)callback
{
NSMutableArray *shareObject = [NSMutableArray array];
NSString *text = args[@"text"];
NSString *hashtag = args[@"hashtag"];
NSURL *url = args[@"url"];
NSObject *file = args[@"file"];
NSArray *activitiesToExclude = args[@"exclude"];
// Return if no args were passed
if (!text && !url && !image && !file) {
RCTLogError(@"[ActivityView] You must specify a text, url, image, imageBase64 and/or imageUrl.");
return;
}
if (text) {
[shareObject addObject:text];
}
if (hashtag) {
[shareObject addObject:hashtag];
}
if (url) {
[shareObject addObject:url];
}
if (image) {
[shareObject addObject:image];
}
if (file) {
[shareObject addObject:file];
}
UIActivityViewController *activityView = [[UIActivityViewController alloc] initWithActivityItems:shareObject applicationActivities:nil];
activityView.excludedActivityTypes = activitiesToExclude
? [self excludedActivitiesForKeys:activitiesToExclude]
: nil;
// Display the Activity View
UIViewController *ctrl = [[[[UIApplication sharedApplication] delegate] window] rootViewController];
/*
* The `anchor` option takes a view to set as the anchor for the share
* popup to point to, on iPads running iOS 8. If it is not passed, it
* defaults to centering the share popup on screen without any arrows.
* refer: (https://github.com/facebook/react-native/commit/f35fbc2a145f0097142d08920e141ea0cce2c31c)
*/
if ([activityView respondsToSelector:@selector(popoverPresentationController)]) {
activityView.popoverPresentationController.sourceView = ctrl.view;
NSNumber *anchorViewTag = [RCTConvert NSNumber:args[@"anchor"]];
if (anchorViewTag) {
UIView *anchorView = [self.bridge.uiManager viewForReactTag:anchorViewTag];
activityView.popoverPresentationController.sourceRect = [anchorView convertRect:anchorView.bounds toView:ctrl.view];
} else {
CGRect sourceRect = CGRectMake(ctrl.view.center.x, ctrl.view.center.y, 1, 1);
activityView.popoverPresentationController.sourceRect = sourceRect;
activityView.popoverPresentationController.permittedArrowDirections = 0;
}
}
[activityView setCompletionWithItemsHandler:^(NSString *activityType, BOOL completed, NSArray *returnedItems, NSError *activityError) {
NSDictionary *payload = @{
@"activityType": RCTNullIfNil(activityType),
@"completed": @(completed),
@"returnedItems": RCTNullIfNil(returnedItems)
};
callback(@[RCTJSErrorFromNSError(activityError), payload]);
}];
[ctrl presentViewController:activityView animated:YES completion:nil];
}
@end