-
Notifications
You must be signed in to change notification settings - Fork 59
/
ActivityView.m
153 lines (125 loc) · 5.16 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
#import "ActivityView.h"
#import <React/RCTLog.h>
#import <React/RCTBridge.h>
#import <React/RCTUIManager.h>
#import <React/RCTImageLoader.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)
{
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];
}
__weak ActivityView *weakSelf = self;
[self.bridge.imageLoader loadImageWithURLRequest: [RCTConvert NSURLRequest: imageUrl]
callback:^(NSError *error, UIImage *image) {
if (!error) {
dispatch_async([weakSelf methodQueue], ^{
[weakSelf showWithOptions:args image:image];
});
} else {
RCTLogWarn(@"[ActivityView] Could not fetch image.");
}
}];
}
- (void) showWithOptions:(NSDictionary *)args image:(UIImage *)image
{
NSMutableArray *shareObject = [NSMutableArray array];
NSString *text = args[@"text"];
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 (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;
}
}
[ctrl presentViewController:activityView animated:YES completion:nil];
}
@end