Skip to content
This repository was archived by the owner on Mar 16, 2019. It is now read-only.

Commit 91aa192

Browse files
committed
Add IOS UIDocumentInteractionController APIs #108
1 parent 6a4cb4a commit 91aa192

File tree

6 files changed

+91
-22
lines changed

6 files changed

+91
-22
lines changed

src/index.js

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@ import getUUID from './utils/uuid'
2323
import base64 from 'base-64'
2424
import polyfill from './polyfill'
2525
import android from './android'
26+
import ios from './ios'
2627
import JSONStream from './json-stream'
2728
const {
2829
RNFetchBlobSession,
@@ -482,6 +483,7 @@ export default {
482483
fetch,
483484
base64,
484485
android,
486+
ios,
485487
config,
486488
session,
487489
fs,

src/ios.js

Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
// Copyright 2016 wkh237@github. All rights reserved.
2+
// Use of this source code is governed by a MIT-style license that can be
3+
// found in the LICENSE file.
4+
// @flow
5+
6+
import {
7+
NativeModules,
8+
DeviceEventEmitter,
9+
Platform,
10+
NativeAppEventEmitter,
11+
} from 'react-native'
12+
13+
const RNFetchBlob:RNFetchBlobNative = NativeModules.RNFetchBlob
14+
15+
/**
16+
* Open a file using UIDocumentInteractionController
17+
* @param {string]} path Path of the file to be open.
18+
* @param {string} scheme URI scheme that needs to support, optional
19+
* @return {Promise}
20+
*/
21+
function openDocument(path:string, scheme:string) {
22+
if(Platform.OS === 'ios')
23+
return RNFetchBlob.openDocument(path, scheme)
24+
else
25+
return Promise.reject('RNFetchBlob.openDocument only supports IOS.')
26+
}
27+
28+
/**
29+
* Preview a file using UIDocumentInteractionController
30+
* @param {string]} path Path of the file to be open.
31+
* @param {string} scheme URI scheme that needs to support, optional
32+
* @return {Promise}
33+
*/
34+
function previewDocument(path:string, scheme:string) {
35+
if(Platform.OS === 'ios')
36+
return RNFetchBlob.previewDocument(path, scheme)
37+
else
38+
return Promise.reject('RNFetchBlob.previewDocument only supports IOS.')
39+
}
40+
41+
export default {
42+
openDocument,
43+
previewDocument
44+
}

src/ios/RNFetchBlob/RNFetchBlob.h

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,16 +7,17 @@
77
#ifndef RNFetchBlob_h
88
#define RNFetchBlob_h
99
#import "RCTBridgeModule.h"
10+
#import <UIKit/UIKit.h>
1011

1112

12-
@interface RNFetchBlob : NSObject <RCTBridgeModule> {
13+
@interface RNFetchBlob : NSObject <RCTBridgeModule, UIDocumentInteractionControllerDelegate> {
1314

1415
NSString * filePathPrefix;
1516

1617
}
1718

1819
@property (nonatomic) NSString * filePathPrefix;
19-
20+
@property (nonatomic, strong) UIDocumentInteractionController *documentController;
2021

2122
@end
2223

src/ios/RNFetchBlob/RNFetchBlob.m

Lines changed: 37 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,7 @@
2626
@implementation RNFetchBlob
2727

2828
@synthesize filePathPrefix;
29+
@synthesize documentController;
2930
@synthesize bridge = _bridge;
3031

3132
- (dispatch_queue_t) methodQueue {
@@ -367,10 +368,44 @@ - (NSDictionary *)constantsToExport
367368
[RNFetchBlobFS slice:src dest:dest start:start end:end encode:@"" resolver:resolve rejecter:reject];
368369
})
369370

370-
RCT_EXPORT_METHOD(openFile:(NSString*)uri {
371-
[[[RNFetchBlobFS alloc ] init ]openFile:uri];
371+
RCT_EXPORT_METHOD(openDocument:(NSString*)uri scheme:(NSString *)scheme resolver:(RCTPromiseResolveBlock)resolve rejecter:(RCTPromiseRejectBlock)reject
372+
{
373+
374+
NSURL * url = [[NSURL alloc] initWithString:uri];
375+
documentController = [UIDocumentInteractionController interactionControllerWithURL:url];
376+
UIViewController *rootCtrl = [[[[UIApplication sharedApplication] delegate] window] rootViewController];
377+
documentController.delegate = self;
378+
if(scheme == nil || [[UIApplication sharedApplication] canOpenURL:[NSURL URLWithString:scheme]]) {
379+
[documentController presentOpenInMenuFromRect:rootCtrl.view.bounds inView:rootCtrl.view animated:YES];
380+
resolve(@[[NSNull null]]);
381+
} else {
382+
reject(@"RNFetchBlob could not open document", @"scheme is not supported", nil);
383+
}
372384
})
373385

386+
# pragma mark - open file with UIDocumentInteractionController and delegate
387+
388+
RCT_EXPORT_METHOD(previewDocument:(NSString*)uri scheme:(NSString *)scheme resolver:(RCTPromiseResolveBlock)resolve rejecter:(RCTPromiseRejectBlock)reject
389+
{
390+
391+
NSURL * url = [[NSURL alloc] initWithString:uri];
392+
documentController = [UIDocumentInteractionController interactionControllerWithURL:url];
393+
documentController.delegate = self;
394+
395+
if(scheme == nil || [[UIApplication sharedApplication] canOpenURL:[NSURL URLWithString:scheme]]) {
396+
[documentController presentPreviewAnimated:YES];
397+
resolve(@[[NSNull null]]);
398+
} else {
399+
reject(@"RNFetchBlob could not open document", @"scheme is not supported", nil);
400+
}
401+
})
402+
403+
- (UIViewController *) documentInteractionControllerViewControllerForPreview: (UIDocumentInteractionController *) controller {
404+
UIWindow *window = [UIApplication sharedApplication].keyWindow;
405+
return window.rootViewController;
406+
}
407+
408+
374409
#pragma mark RNFetchBlob private methods
375410

376411

src/ios/RNFetchBlobFS.h

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -11,10 +11,9 @@
1111

1212
#import <Foundation/Foundation.h>
1313
#import "RCTBridgeModule.h"
14-
#import <UIKit/UIKit.h>
1514
@import AssetsLibrary;
1615

17-
@interface RNFetchBlobFS : NSObject <NSStreamDelegate, UIDocumentInteractionControllerDelegate> {
16+
@interface RNFetchBlobFS : NSObject <NSStreamDelegate> {
1817
NSOutputStream * outStream;
1918
NSInputStream * inStream;
2019
RCTResponseSenderBlock callback;

src/ios/RNFetchBlobFS.m

Lines changed: 4 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,10 @@
2424
// File system access methods
2525
//
2626
////////////////////////////////////////
27-
27+
@interface RNFetchBlobFS() {
28+
UIDocumentInteractionController * docCtrl;
29+
}
30+
@end
2831
@implementation RNFetchBlobFS
2932

3033

@@ -730,19 +733,4 @@ + (void) getPathFromUri:(NSString *)uri completionHandler:(void(^)(NSString * pa
730733
}
731734
}
732735

733-
# pragma mark - open file with UIDocumentInteractionController and delegate
734-
735-
- (void) openFile:(NSString *) uri
736-
{
737-
NSURL * url = [[NSURL alloc] initWithString:uri];
738-
UIDocumentInteractionController * docCtrl = [UIDocumentInteractionController interactionControllerWithURL:url];
739-
docCtrl.delegate = self;
740-
[docCtrl presentPreviewAnimated:YES];
741-
742-
}
743-
744-
- (UIViewController *) documentInteractionControllerViewControllerForPreview: (UIDocumentInteractionController *) controller {
745-
return self;
746-
}
747-
748736
@end

0 commit comments

Comments
 (0)