Skip to content

Commit b89e2c7

Browse files
Markus Petterssonchristopherdro
authored andcommitted
Updated from UIWebView to WKWebview (#147)
1 parent e856527 commit b89e2c7

File tree

2 files changed

+22
-23
lines changed

2 files changed

+22
-23
lines changed

ios/RNHTMLtoPDF/RNHTMLtoPDF.h

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33

44
#import <React/RCTView.h>
55
#import <React/RCTBridgeModule.h>
6-
7-
@interface RNHTMLtoPDF : RCTView <RCTBridgeModule, UIWebViewDelegate>
6+
#import <WebKit/WebKit.h>
7+
@interface RNHTMLtoPDF : RCTView <RCTBridgeModule, WKNavigationDelegate>
88

99
@end

ios/RNHTMLtoPDF/RNHTMLtoPDF.m

Lines changed: 20 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -52,7 +52,7 @@ @implementation RNHTMLtoPDF {
5252
UIColor *_bgColor;
5353
NSInteger *_numberOfPages;
5454
CGSize _PDFSize;
55-
UIWebView *_webView;
55+
WKWebView *_webView;
5656
float _paddingBottom;
5757
float _paddingTop;
5858
float _paddingLeft;
@@ -73,8 +73,8 @@ + (BOOL)requiresMainQueueSetup
7373
- (instancetype)init
7474
{
7575
if (self = [super init]) {
76-
_webView = [[UIWebView alloc] initWithFrame:self.bounds];
77-
_webView.delegate = self;
76+
_webView = [[WKWebView alloc] initWithFrame:self.bounds];
77+
_webView.navigationDelegate = self;
7878
[self addSubview:_webView];
7979
autoHeight = false;
8080
}
@@ -173,44 +173,43 @@ - (instancetype)init
173173

174174
NSString *path = [[NSBundle mainBundle] bundlePath];
175175
NSURL *baseURL = [NSURL fileURLWithPath:path];
176-
177-
[_webView loadHTMLString:_html baseURL:baseURL];
176+
dispatch_async(dispatch_get_main_queue(), ^{
177+
[_webView loadHTMLString:_html baseURL:baseURL];
178+
});
178179

179180
_resolveBlock = resolve;
180181
_rejectBlock = reject;
181182

182183
}
183-
184-
- (void)webViewDidFinishLoad:(UIWebView *)awebView
185-
{
186-
if (awebView.isLoading)
187-
return;
188-
184+
-(void)webView:(WKWebView *)webView didFinishNavigation:(WKNavigation *)navigation {
185+
if (webView.isLoading)
186+
return;
187+
189188
UIPrintPageRenderer *render = [[UIPrintPageRenderer alloc] init];
190-
[render addPrintFormatter:awebView.viewPrintFormatter startingAtPageAtIndex:0];
191-
189+
[render addPrintFormatter:webView.viewPrintFormatter startingAtPageAtIndex:0];
190+
192191
// Define the printableRect and paperRect
193192
// If the printableRect defines the printable area of the page
194193
CGRect paperRect = CGRectMake(0, 0, _PDFSize.width, _PDFSize.height);
195194
CGRect printableRect = CGRectMake(_paddingTop, _paddingLeft, _PDFSize.width-(_paddingLeft + _paddingRight), _PDFSize.height-(_paddingBottom + _paddingTop));
196-
197-
195+
196+
198197
[render setValue:[NSValue valueWithCGRect:paperRect] forKey:@"paperRect"];
199198
[render setValue:[NSValue valueWithCGRect:printableRect] forKey:@"printableRect"];
200-
199+
201200
NSData * pdfData = [render printToPDF:&_numberOfPages backgroundColor:_bgColor ];
202-
201+
203202
if (pdfData) {
204203
NSString *pdfBase64 = @"";
205-
204+
206205
[pdfData writeToFile:_filePath atomically:YES];
207206
if (_base64) {
208207
pdfBase64 = [pdfData base64EncodedStringWithOptions:0];
209208
}
210209
NSDictionary *data = [NSDictionary dictionaryWithObjectsAndKeys:
211-
pdfBase64, @"base64",
212-
[NSString stringWithFormat: @"%ld", (long)_numberOfPages], @"numberOfPages",
213-
_filePath, @"filePath", nil];
210+
pdfBase64, @"base64",
211+
[NSString stringWithFormat: @"%ld", (long)_numberOfPages], @"numberOfPages",
212+
_filePath, @"filePath", nil];
214213
_resolveBlock(data);
215214
} else {
216215
NSError *error;

0 commit comments

Comments
 (0)