forked from naoufal/react-native-activity-view
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathActivityView.m
More file actions
128 lines (108 loc) · 4.69 KB
/
ActivityView.m
File metadata and controls
128 lines (108 loc) · 4.69 KB
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
#import "ActivityView.h"
#import "RCTLog.h"
#import "RCTBridge.h"
#import "RCTUIManager.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)
{
NSMutableArray *shareObject = [NSMutableArray array];
NSString *text = args[@"text"];
NSURL *url = args[@"url"];
NSString *imageUrl = args[@"imageUrl"];
NSArray *activitiesToExclude = args[@"exclude"];
NSString *image = args[@"image"];
NSString *imageBase64 = args[@"imageBase64"];
NSData *imageData;
// Try to fetch image
if (imageUrl) {
@try {
imageData = [[NSData alloc] initWithContentsOfURL: [NSURL URLWithString: imageUrl]];
} @catch (NSException *exception) {
RCTLogWarn(@"[ActivityView] Could not fetch image.");
}
}
if (imageBase64) {
@try {
imageData = [[NSData alloc] initWithBase64EncodedString:imageBase64 options:NSDataBase64DecodingIgnoreUnknownCharacters];
} @catch (NSException *exception) {
RCTLogWarn(@"[ActivityView] Could not decode image");
}
}
// Return if no args were passed
if (!text && !url && !image && !imageData) {
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: [UIImage imageNamed: image]];
} else if (imageData) {
[shareObject addObject: [UIImage imageWithData: imageData]];
}
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.presentedViewController presentViewController:activityView animated:YES completion:nil];
}
@end