Bladeren bron

Add IOS UIDocumentInteractionController APIs #108

Ben Hsieh 7 jaren geleden
bovenliggende
commit
91aa192bc4
6 gewijzigde bestanden met toevoegingen van 91 en 22 verwijderingen
  1. 2
    0
      src/index.js
  2. 44
    0
      src/ios.js
  3. 3
    2
      src/ios/RNFetchBlob/RNFetchBlob.h
  4. 37
    2
      src/ios/RNFetchBlob/RNFetchBlob.m
  5. 1
    2
      src/ios/RNFetchBlobFS.h
  6. 4
    16
      src/ios/RNFetchBlobFS.m

+ 2
- 0
src/index.js Bestand weergeven

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

+ 44
- 0
src/ios.js Bestand weergeven

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 Bestand weergeven

7
 #ifndef RNFetchBlob_h
7
 #ifndef RNFetchBlob_h
8
 #define RNFetchBlob_h
8
 #define RNFetchBlob_h
9
 #import "RCTBridgeModule.h"
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
     NSString * filePathPrefix;
15
     NSString * filePathPrefix;
15
 
16
 
16
 }
17
 }
17
 
18
 
18
 @property (nonatomic) NSString * filePathPrefix;
19
 @property (nonatomic) NSString * filePathPrefix;
19
-
20
+@property (nonatomic, strong) UIDocumentInteractionController *documentController;
20
 
21
 
21
 @end
22
 @end
22
 
23
 

+ 37
- 2
src/ios/RNFetchBlob/RNFetchBlob.m Bestand weergeven

26
 @implementation RNFetchBlob
26
 @implementation RNFetchBlob
27
 
27
 
28
 @synthesize filePathPrefix;
28
 @synthesize filePathPrefix;
29
+@synthesize documentController;
29
 @synthesize bridge = _bridge;
30
 @synthesize bridge = _bridge;
30
 
31
 
31
 - (dispatch_queue_t) methodQueue {
32
 - (dispatch_queue_t) methodQueue {
367
     [RNFetchBlobFS slice:src dest:dest start:start end:end encode:@"" resolver:resolve rejecter:reject];
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
 #pragma mark RNFetchBlob private methods
409
 #pragma mark RNFetchBlob private methods
375
 
410
 
376
 
411
 

+ 1
- 2
src/ios/RNFetchBlobFS.h Bestand weergeven

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

+ 4
- 16
src/ios/RNFetchBlobFS.m Bestand weergeven

24
 //  File system access methods
24
 //  File system access methods
25
 //
25
 //
26
 ////////////////////////////////////////
26
 ////////////////////////////////////////
27
-
27
+@interface RNFetchBlobFS() {
28
+    UIDocumentInteractionController * docCtrl;
29
+}
30
+@end
28
 @implementation RNFetchBlobFS
31
 @implementation RNFetchBlobFS
29
 
32
 
30
 
33
 
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
 @end
736
 @end