Browse Source

Add IOS UIDocumentInteractionController APIs #108

Ben Hsieh 7 years ago
parent
commit
91aa192bc4

+ 2
- 0
src/index.js View File

@@ -23,6 +23,7 @@ import getUUID from './utils/uuid'
23 23
 import base64 from 'base-64'
24 24
 import polyfill from './polyfill'
25 25
 import android from './android'
26
+import ios from './ios'
26 27
 import JSONStream from './json-stream'
27 28
 const {
28 29
   RNFetchBlobSession,
@@ -482,6 +483,7 @@ export default {
482 483
   fetch,
483 484
   base64,
484 485
   android,
486
+  ios,
485 487
   config,
486 488
   session,
487 489
   fs,

+ 44
- 0
src/ios.js View File

@@ -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
+}

+ 3
- 2
src/ios/RNFetchBlob/RNFetchBlob.h View File

@@ -7,16 +7,17 @@
7 7
 #ifndef RNFetchBlob_h
8 8
 #define RNFetchBlob_h
9 9
 #import "RCTBridgeModule.h"
10
+#import <UIKit/UIKit.h>
10 11
 
11 12
 
12
-@interface RNFetchBlob : NSObject <RCTBridgeModule> {
13
+@interface RNFetchBlob : NSObject <RCTBridgeModule, UIDocumentInteractionControllerDelegate> {
13 14
 
14 15
     NSString * filePathPrefix;
15 16
 
16 17
 }
17 18
 
18 19
 @property (nonatomic) NSString * filePathPrefix;
19
-
20
+@property (nonatomic, strong) UIDocumentInteractionController *documentController;
20 21
 
21 22
 @end
22 23
 

+ 37
- 2
src/ios/RNFetchBlob/RNFetchBlob.m View File

@@ -26,6 +26,7 @@
26 26
 @implementation RNFetchBlob
27 27
 
28 28
 @synthesize filePathPrefix;
29
+@synthesize documentController;
29 30
 @synthesize bridge = _bridge;
30 31
 
31 32
 - (dispatch_queue_t) methodQueue {
@@ -367,10 +368,44 @@ RCT_EXPORT_METHOD(slice:(NSString *)src dest:(NSString *)dest start:(nonnull NSN
367 368
     [RNFetchBlobFS slice:src dest:dest start:start end:end encode:@"" resolver:resolve rejecter:reject];
368 369
 })
369 370
 
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
+    }
372 384
 })
373 385
 
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
+
374 409
 #pragma mark RNFetchBlob private methods
375 410
 
376 411
 

+ 1
- 2
src/ios/RNFetchBlobFS.h View File

@@ -11,10 +11,9 @@
11 11
 
12 12
 #import <Foundation/Foundation.h>
13 13
 #import "RCTBridgeModule.h"
14
-#import <UIKit/UIKit.h>
15 14
 @import AssetsLibrary;
16 15
 
17
-@interface RNFetchBlobFS : NSObject <NSStreamDelegate, UIDocumentInteractionControllerDelegate>  {
16
+@interface RNFetchBlobFS : NSObject <NSStreamDelegate>  {
18 17
     NSOutputStream * outStream;
19 18
     NSInputStream * inStream;
20 19
     RCTResponseSenderBlock callback;

+ 4
- 16
src/ios/RNFetchBlobFS.m View File

@@ -24,7 +24,10 @@ NSMutableDictionary *fileStreams = nil;
24 24
 //  File system access methods
25 25
 //
26 26
 ////////////////////////////////////////
27
-
27
+@interface RNFetchBlobFS() {
28
+    UIDocumentInteractionController * docCtrl;
29
+}
30
+@end
28 31
 @implementation RNFetchBlobFS
29 32
 
30 33
 
@@ -730,19 +733,4 @@ NSMutableDictionary *fileStreams = nil;
730 733
     }
731 734
 }
732 735
 
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
-
748 736
 @end