Skip to content

Commit e275a84

Browse files
authored
Merge pull request #7 from doomsower/presentOpenInMenu
Present open in menu
2 parents b385839 + a8d46d6 commit e275a84

File tree

3 files changed

+78
-29
lines changed

3 files changed

+78
-29
lines changed

index.d.ts

Lines changed: 26 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -441,14 +441,37 @@ export interface IOSApi {
441441
* Open a file in {@link https://developer.apple.com/reference/uikit/uidocumentinteractioncontroller UIDocumentInteractionController},
442442
* this is the default document viewer of iOS, supports several kinds of files. On Android, there's an similar method {@link android.actionViewIntent}.
443443
* @param path This is a required field, the path to the document. The path should NOT contains any scheme prefix.
444+
* @param {string} scheme URI scheme that needs to support, optional
444445
*/
445-
previewDocument(path: string): void;
446+
previewDocument(path: string, scheme?: string): void;
446447

447448
/**
448449
* Show options menu for interact with the file.
449450
* @param path This is a required field, the path to the document. The path should NOT contains any scheme prefix.
451+
* @param {string} scheme URI scheme that needs to support, optional
450452
*/
451-
openDocument(path: string): void;
453+
openDocument(path: string, scheme?: string): void;
454+
455+
/**
456+
* Displays an options menu using [UIDocumentInteractionController](https://developer.apple.com/reference/uikit/uidocumentinteractioncontroller).[presentOptionsMenu](https://developer.apple.com/documentation/uikit/uidocumentinteractioncontroller/1616814-presentoptionsmenu)
457+
* @param {string} path Path of the file to be open.
458+
* @param {string} scheme URI scheme that needs to support, optional
459+
*/
460+
presentOptionsMenu(path: string, scheme?: string): void;
461+
462+
/**
463+
* Displays a menu for opening the document using [UIDocumentInteractionController](https://developer.apple.com/reference/uikit/uidocumentinteractioncontroller).[presentOpenInMenu](https://developer.apple.com/documentation/uikit/uidocumentinteractioncontroller/1616807-presentopeninmenu)
464+
* @param {string} path Path of the file to be open.
465+
* @param {string} scheme URI scheme that needs to support, optional
466+
*/
467+
presentOpenInMenu(path: string, scheme?: string): void;
468+
469+
/**
470+
* Displays a full-screen preview of the target document using [UIDocumentInteractionController](https://developer.apple.com/reference/uikit/uidocumentinteractioncontroller).[presentPreview](https://developer.apple.com/documentation/uikit/uidocumentinteractioncontroller/1616828-presentpreview)
471+
* @param {string} path Path of the file to be open.
472+
* @param {string} scheme URI scheme that needs to support, optional
473+
*/
474+
presentPreview(path: string, scheme?: string): void;
452475
}
453476

454477
export interface AndroidDownloadOption {
@@ -475,7 +498,7 @@ export interface AndroidDownloadOption {
475498
/**
476499
* Boolean value that determines if notification will be displayed.
477500
*/
478-
showNotification: boolean
501+
showNotification: boolean
479502
}
480503

481504
export interface AndroidApi {

ios.js

Lines changed: 32 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -2,39 +2,44 @@
22
// Use of this source code is governed by a MIT-style license that can be
33
// found in the LICENSE file.
44

5-
import {
6-
NativeModules,
7-
DeviceEventEmitter,
8-
Platform,
9-
NativeAppEventEmitter,
10-
} from 'react-native'
5+
import { NativeModules, Platform } from "react-native";
116

12-
const RNFetchBlob:RNFetchBlobNative = NativeModules.RNFetchBlob
7+
const RNFetchBlob: RNFetchBlobNative = NativeModules.RNFetchBlob;
138

149
/**
15-
* Open a file using UIDocumentInteractionController
10+
* Displays an options menu using UIDocumentInteractionController.presentOptionsMenu
1611
* @param {string} path Path of the file to be open.
1712
* @param {string} scheme URI scheme that needs to support, optional
1813
* @return {Promise}
1914
*/
20-
function previewDocument(path:string, scheme:string) {
21-
if(Platform.OS === 'ios')
22-
return RNFetchBlob.previewDocument('file://' + path, scheme)
23-
else
24-
return Promise.reject('RNFetchBlob.openDocument only supports IOS.')
15+
function presentOptionsMenu(path: string, scheme: string) {
16+
if (Platform.OS === "ios")
17+
return RNFetchBlob.presentOptionsMenu("file://" + path, scheme);
18+
else return Promise.reject("RNFetchBlob.openDocument only supports IOS.");
2519
}
2620

2721
/**
28-
* Preview a file using UIDocumentInteractionController
22+
* Displays a menu for opening the document using UIDocumentInteractionController.presentOpenInMenu
2923
* @param {string} path Path of the file to be open.
3024
* @param {string} scheme URI scheme that needs to support, optional
3125
* @return {Promise}
3226
*/
33-
function openDocument(path:string, scheme:string) {
34-
if(Platform.OS === 'ios')
35-
return RNFetchBlob.openDocument('file://' + path, scheme)
36-
else
37-
return Promise.reject('RNFetchBlob.previewDocument only supports IOS.')
27+
function presentOpenInMenu(path: string, scheme: string) {
28+
if (Platform.OS === "ios")
29+
return RNFetchBlob.presentOpenInMenu("file://" + path, scheme);
30+
else return Promise.reject("RNFetchBlob.openDocument only supports IOS.");
31+
}
32+
33+
/**
34+
* Displays a full-screen preview of the target document using UIDocumentInteractionController.presentPreview
35+
* @param {string} path Path of the file to be open.
36+
* @param {string} scheme URI scheme that needs to support, optional
37+
* @return {Promise}
38+
*/
39+
function presentPreview(path: string, scheme: string) {
40+
if (Platform.OS === "ios")
41+
return RNFetchBlob.presentPreview("file://" + path, scheme);
42+
else return Promise.reject("RNFetchBlob.previewDocument only supports IOS.");
3843
}
3944

4045
/**
@@ -43,12 +48,15 @@ function openDocument(path:string, scheme:string) {
4348
* @param {string} url URL of the resource, only file URL is supported
4449
* @return {Promise}
4550
*/
46-
function excludeFromBackupKey(path:string) {
47-
return RNFetchBlob.excludeFromBackupKey('file://' + path);
51+
function excludeFromBackupKey(path: string) {
52+
return RNFetchBlob.excludeFromBackupKey("file://" + path);
4853
}
4954

5055
export default {
51-
openDocument,
52-
previewDocument,
56+
presentPreview,
57+
openDocument: presentPreview, // legacy alias
58+
presentOptionsMenu,
59+
previewDocument: presentOptionsMenu, // legacy alias
60+
presentOpenInMenu,
5361
excludeFromBackupKey
54-
}
62+
};

ios/RNFetchBlob/RNFetchBlob.m

Lines changed: 20 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -573,7 +573,7 @@ - (NSDictionary *)constantsToExport
573573
[RNFetchBlobFS slice:src dest:dest start:start end:end encode:@"" resolver:resolve rejecter:reject];
574574
}
575575

576-
RCT_EXPORT_METHOD(previewDocument:(NSString*)uri scheme:(NSString *)scheme resolver:(RCTPromiseResolveBlock)resolve rejecter:(RCTPromiseRejectBlock)reject)
576+
RCT_EXPORT_METHOD(presentOptionsMenu:(NSString*)uri scheme:(NSString *)scheme resolver:(RCTPromiseResolveBlock)resolve rejecter:(RCTPromiseRejectBlock)reject)
577577
{
578578
NSString * utf8uri = [uri stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding];
579579
NSURL * url = [[NSURL alloc] initWithString:utf8uri];
@@ -592,9 +592,27 @@ - (NSDictionary *)constantsToExport
592592
}
593593
}
594594

595+
RCT_EXPORT_METHOD(presentOpenInMenu:(NSString*)uri scheme:(NSString *)scheme resolver:(RCTPromiseResolveBlock)resolve rejecter:(RCTPromiseRejectBlock)reject)
596+
{
597+
NSString * utf8uri = [uri stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding];
598+
NSURL * url = [[NSURL alloc] initWithString:utf8uri];
599+
documentController = [UIDocumentInteractionController interactionControllerWithURL:url];
600+
UIViewController *rootCtrl = [[[[UIApplication sharedApplication] delegate] window] rootViewController];
601+
documentController.delegate = self;
602+
if(scheme == nil || [[UIApplication sharedApplication] canOpenURL:[NSURL URLWithString:scheme]]) {
603+
CGRect rect = CGRectMake(0.0, 0.0, 0.0, 0.0);
604+
dispatch_sync(dispatch_get_main_queue(), ^{
605+
[documentController presentOpenInMenuFromRect:rect inView:rootCtrl.view animated:YES];
606+
});
607+
resolve(@[[NSNull null]]);
608+
} else {
609+
reject(@"EINVAL", @"scheme is not supported", nil);
610+
}
611+
}
612+
595613
# pragma mark - open file with UIDocumentInteractionController and delegate
596614

597-
RCT_EXPORT_METHOD(openDocument:(NSString*)uri scheme:(NSString *)scheme resolver:(RCTPromiseResolveBlock)resolve rejecter:(RCTPromiseRejectBlock)reject)
615+
RCT_EXPORT_METHOD(presentPreview:(NSString*)uri scheme:(NSString *)scheme resolver:(RCTPromiseResolveBlock)resolve rejecter:(RCTPromiseRejectBlock)reject)
598616
{
599617
NSString * utf8uri = [uri stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding];
600618
NSURL * url = [[NSURL alloc] initWithString:utf8uri];

0 commit comments

Comments
 (0)