Skip to content

Commit af2ee0a

Browse files
authored
Merge pull request #2 from dansiemens/pdf
PDF Support
2 parents 6735eef + 353f9d3 commit af2ee0a

File tree

3 files changed

+41
-2
lines changed

3 files changed

+41
-2
lines changed

android/src/main/java/com/alinz/parkerdan/shareextension/RealPathUtil.java

Lines changed: 18 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -178,14 +178,31 @@ public static boolean isWhatsappFile(Uri uri) {
178178

179179
private static String copyFile(Context context, Uri uri) {
180180

181+
String extension;
182+
String mimeType = context.getContentResolver().getType(uri);
183+
184+
switch (mimeType) {
185+
case "image/jpeg":
186+
extension = ".jpg";
187+
break;
188+
case "image/png":
189+
extension = ".png";
190+
break;
191+
case "application/pdf":
192+
extension = ".pdf";
193+
break;
194+
default:
195+
extension = ".jpg";
196+
}
197+
181198
String filePath;
182199
InputStream inputStream = null;
183200
BufferedOutputStream outStream = null;
184201
try {
185202
inputStream = context.getContentResolver().openInputStream(uri);
186203

187204
File extDir = context.getExternalFilesDir(null);
188-
filePath = extDir.getAbsolutePath() + "/IMG_" + UUID.randomUUID().toString() + ".jpg";
205+
filePath = extDir.getAbsolutePath() + "/IMG_" + UUID.randomUUID().toString() + extension;
189206
outStream = new BufferedOutputStream(new FileOutputStream
190207
(filePath));
191208

android/src/main/java/com/alinz/parkerdan/shareextension/ShareModule.java

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -46,6 +46,7 @@ public WritableArray processIntent() {
4646
mediaTypesSupported.add("video");
4747
mediaTypesSupported.add("audio");
4848
mediaTypesSupported.add("image");
49+
mediaTypesSupported.add("application/pdf");
4950

5051
String type = "";
5152
String action = "";
@@ -68,7 +69,7 @@ public WritableArray processIntent() {
6869
dataMap.putString("value", intent.getStringExtra(Intent.EXTRA_TEXT));
6970
dataArrayMap.pushMap(dataMap);
7071
} else if (Intent.ACTION_SEND.equals(action) && (
71-
mediaTypesSupported.contains(typePart))) {
72+
mediaTypesSupported.contains(typePart) || mediaTypesSupported.contains(type))) {
7273
WritableMap dataMap = Arguments.createMap();
7374
dataMap.putString("type", type);
7475
Uri uri = (Uri) intent.getParcelableExtra(Intent.EXTRA_STREAM);

ios/ReactNativeShareExtension.m

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
#define URL_IDENTIFIER @"public.url"
66
#define IMAGE_IDENTIFIER @"public.image"
77
#define TEXT_IDENTIFIER (NSString *)kUTTypePlainText
8+
#define PDF_IDENTIFIER (NSString *)kUTTypePDF
89

910
NSExtensionContext* extensionContext;
1011

@@ -14,6 +15,11 @@ @implementation ReactNativeShareExtension {
1415
NSString* value;
1516
}
1617

18+
+ (BOOL)requiresMainQueueSetup
19+
{
20+
return YES;
21+
}
22+
1723
- (UIView*) shareView {
1824
return nil;
1925
}
@@ -74,6 +80,7 @@ - (void)extractDataFromContext:(NSExtensionContext *)context withCallback:(void(
7480
__block NSItemProvider *urlProvider = nil;
7581
__block NSItemProvider *imageProvider = nil;
7682
__block NSItemProvider *textProvider = nil;
83+
__block NSItemProvider *docProvider = nil;
7784
__block NSUInteger index = 0;
7885

7986
[attachments enumerateObjectsUsingBlock:^(NSItemProvider *provider, NSUInteger idx, BOOL *stop) {
@@ -117,6 +124,20 @@ - (void)extractDataFromContext:(NSExtensionContext *)context withCallback:(void(
117124
}
118125

119126
}];
127+
} else if ([provider hasItemConformingToTypeIdentifier:PDF_IDENTIFIER]){
128+
docProvider = provider;
129+
index += 1;
130+
[docProvider loadItemForTypeIdentifier:PDF_IDENTIFIER options:nil completionHandler:^(id<NSSecureCoding> item, NSError *error) {
131+
NSURL *url = (NSURL *)item;
132+
[itemArray addObject: @{
133+
@"type": @"application/pdf",
134+
@"value": [url absoluteString]
135+
}];
136+
137+
if (callback && (index == [attachments count])) {
138+
callback(itemArray, nil);
139+
}
140+
}];
120141
} else if([provider hasItemConformingToTypeIdentifier:URL_IDENTIFIER]) {
121142
urlProvider = provider;
122143
index += 1;

0 commit comments

Comments
 (0)