-
Notifications
You must be signed in to change notification settings - Fork 178
/
Copy pathRNReactNativeDocViewer.m
94 lines (76 loc) · 2.63 KB
/
RNReactNativeDocViewer.m
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
//
// RNReactNativeDocViewer.m
// RNReactNativeDocViewer
//
// Created by Philipp Hecht on 10/03/17.
// Copyright (c) 2017 Philipp Hecht. All rights reserved.
//
#import <UIKit/UIKit.h>
#import "RNReactNativeDocViewer.h"
#if __has_include("RCTLog.h")
#import "RCTLog.h"
#else
#import <React/RCTLog.h>
#endif
@implementation RNReactNativeDocViewer
RCT_EXPORT_MODULE()
- (dispatch_queue_t)methodQueue
{
return dispatch_get_main_queue();
}
RCT_EXPORT_METHOD(testModule:(NSString *)name location:(NSString *)location)
{
RCTLogInfo(@"TEST Module %@ at %@", name, location);
}
RCT_EXPORT_METHOD(openDoc:(NSArray *)array callback:(RCTResponseSenderBlock)callback)
{
__weak RNReactNativeDocViewer* weakSelf = self;
dispatch_queue_t asyncQueue = dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0);
dispatch_async(asyncQueue, ^{
NSDictionary* dict = [array objectAtIndex:0];
NSString* urlStr = dict[@"url"];
NSString* filename = dict[@"fileName"];
NSURL* url = [NSURL URLWithString:urlStr];
RCTLogInfo(@"Pretending to create an event at %@", url);
NSData* dat = [NSData dataWithContentsOfURL:url];
if (dat == nil) {
if (callback) {
callback(@[[NSNull null], @"DATA nil"]);
}
return;
}
NSString* fileName = [url lastPathComponent];
NSString* fileExt = [fileName pathExtension];
if([fileExt length] == 0){
fileName = [NSString stringWithFormat:@"%@%@", fileName, @".pdf"];
}
NSString* path = [NSTemporaryDirectory() stringByAppendingPathComponent: fileName];
NSURL* tmpFileUrl = [[NSURL alloc] initFileURLWithPath:path];
[dat writeToURL:tmpFileUrl atomically:YES];
weakSelf.fileUrl = tmpFileUrl;
dispatch_async(dispatch_get_main_queue(), ^{
QLPreviewController* cntr = [[QLPreviewController alloc] init];
cntr.delegate = weakSelf;
cntr.dataSource = weakSelf;
if (callback) {
callback(@[[NSNull null], array]);
}
UIViewController* root = [[[UIApplication sharedApplication] keyWindow] rootViewController];
[root presentViewController:cntr animated:YES completion:nil];
});
});
}
- (NSInteger) numberOfPreviewItemsInPreviewController: (QLPreviewController *) controller
{
return 1;
}
- (id <QLPreviewItem>) previewController: (QLPreviewController *) controller previewItemAtIndex: (NSInteger) index
{
return self;
}
#pragma mark - QLPreviewItem protocol
- (NSURL*)previewItemURL
{
return self.fileUrl;
}
@end