Repository navigation
Expand file tree
/
Copy pathPDFView.m
More file actions
154 lines (130 loc) · 4.84 KB
/
Copy pathPDFView.m
File metadata and controls
154 lines (130 loc) · 4.84 KB
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
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
#import "PDFView.h"
@implementation PDFView
{
WKWebView *webview;
NSString *currentResource;
NSString *currentResourceType;
bool didLoadOnce; // Needed as on init, didSetProps is called as well, leading to layoutSubviews being called twice
}
- (void)didSetProps:(NSArray<NSString *> *)changedProps {
if (didLoadOnce) {
[self layoutSubviews];
}
}
- (instancetype)init {
self = [super init];
if ( self ) {
[self setBackgroundColor: [UIColor clearColor]];
webview = [[WKWebView alloc] initWithFrame: self.frame];
[webview setNavigationDelegate: self];
[self addSubview: webview];
}
return self;
}
- (void)layoutSubviews {
[super layoutSubviews];
[webview setAlpha: 0.0];
webview.frame = self.bounds;
didLoadOnce = true;
if (![self isNewInput]) {
return;
}
if (![self isRequiredInputSet]) {
return;
}
[self updateInput];
if (![self isSupportedResourceType]) {
[self throwError: ERROR_UNSUPPORTED_TYPE withMessage: [NSString stringWithFormat:@"resourceType: %@ not recognized", _resourceType]];
return;
}
if ([self isURLResource]) {
NSURLRequest *request = [NSURLRequest requestWithURL: [NSURL URLWithString: _resource]];
[webview loadRequest: request];
} else if ([self isFileResource]) {
NSURL *targetURL = [self fileFromBundleURL];
if (targetURL == nil) {
targetURL = [self fileFromDocumentsDirectoryURL];
}
if (targetURL == nil) {
[self throwError: ERROR_ONLOADING withMessage: ERROR_MESSAGE_FILENOTFOUND];
return;
}
NSURLRequest *request = [NSURLRequest requestWithURL: targetURL];
[webview loadRequest: request];
} else {
NSString *characterEncodingName = [_textEncoding isEqual: UTF_16] ? UTF_16 : UTF_8;
NSData *base64Decoded = [[NSData alloc] initWithBase64EncodedString: _resource options: NSDataBase64DecodingIgnoreUnknownCharacters];
if (base64Decoded != nil) {
[webview loadData: base64Decoded MIMEType: MIMETYPE_PDF characterEncodingName: characterEncodingName baseURL: [[NSURL alloc] init]];
} else {
[self throwError: ERROR_ONLOADING withMessage: ERROR_MESSAGE_BASE64_NIL];
}
}
}
- (void)updateInput {
currentResourceType = _resourceType;
currentResource = _resource;
}
- (void)webView:(WKWebView *)webView didFinishNavigation:(WKNavigation *)navigation {
[UIView animateWithDuration: _fadeInDuration animations: ^(void) {
[webview setAlpha: 1.0];
}];
if (_onLoad) {
_onLoad(nil);
}
}
- (void)webView:(WKWebView *)webView didFailNavigation:(WKNavigation *)navigation withError:(NSError *)error {
[self throwError: ERROR_ONLOADING withMessage: error.localizedDescription];
}
- (NSURL *)fileFromBundleURL {
NSString *resourcePath = [_resource stringByReplacingOccurrencesOfString:@".pdf" withString: @""]; // Remove pdf extension from path if present
if (![[NSBundle mainBundle] pathForResource: resourcePath ofType: @"pdf"]) {
return nil;
} else {
return [NSURL fileURLWithPath: [[NSBundle mainBundle] pathForResource: resourcePath ofType: @"pdf"]];
}
}
- (NSURL *)fileFromDocumentsDirectoryURL {
NSString *documentsDirectory = [NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES) lastObject];
if (documentsDirectory == nil) {
return nil;
}
NSString *filePath = [documentsDirectory stringByAppendingPathComponent: _resource];
if(![[NSFileManager defaultManager] fileExistsAtPath: filePath]) {
return nil;
}
return [NSURL fileURLWithPath:filePath];
}
- (BOOL)isRequiredInputSet {
if ([_resource length] == 0 || [_resourceType length] == 0) {
NSString* errorMessage = [NSString stringWithFormat:@"resource: %@. resourceType: %@.", _resource, _resourceType];
[self throwError: ERROR_REQUIRED_INPUT_NOT_SET withMessage: errorMessage];
return NO;
}
return YES;
}
- (BOOL)isNewInput {
return ![_resource isEqualToString: currentResource] || ![_resourceType isEqualToString: currentResourceType];
}
- (BOOL)isSupportedResourceType {
return [self isURLResource] || [self isBase64Resource] || [self isFileResource];
}
- (BOOL)isURLResource {
return [_resourceType isEqualToString: RESOURCE_TYPE_URL];
}
- (BOOL)isBase64Resource {
return [_resourceType isEqualToString: RESOURCE_TYPE_BASE64];
}
- (BOOL)isFileResource {
return [_resourceType isEqualToString: RESOURCE_TYPE_FILE];
}
- (void)throwError: (NSString *)title withMessage:(NSString *)message {
if (_onError) {
_onError(@{
title: @{
ERROR_MESSAGE_KEY: message,
}
});
}
}
@end