From 25e56ccc89d330c89c9a07d81cab1bd6854bf546 Mon Sep 17 00:00:00 2001 From: imgx64 Date: Mon, 27 Nov 2017 08:43:49 +0300 Subject: [PATCH] Use WKWebKit instead of UIWebView Fixes #20 This breaks backwards compatibility in the saveWebViewAsPdf methods --- BNHtmlPdfKit.h | 5 +++-- BNHtmlPdfKit.m | 49 +++++++++++++++++++++++++------------------------ 2 files changed, 28 insertions(+), 26 deletions(-) diff --git a/BNHtmlPdfKit.h b/BNHtmlPdfKit.h index 418720f..687975f 100644 --- a/BNHtmlPdfKit.h +++ b/BNHtmlPdfKit.h @@ -6,6 +6,7 @@ // #import +#import @protocol BNHtmlPdfKitDelegate; @@ -318,7 +319,7 @@ Saves an webView to PDF data. @param webView The webView to save as a pdf. */ -- (void)saveWebViewAsPdf:(UIWebView *)webView; +- (void)saveWebViewAsPdf:(WKWebView *)webView; /** Saves webView content to a PDF file. @@ -326,7 +327,7 @@ Saves webView content to a PDF file. @param webView The webView to save as a pdf file. @param file The filename of the pdf file to save. */ -- (void)saveWebViewAsPdf:(UIWebView *)webView toFile:(NSString *)file; +- (void)saveWebViewAsPdf:(WKWebView *)webView toFile:(NSString *)file; /** Determine the preferred paper size for general printing. From Pierre Bernard. diff --git a/BNHtmlPdfKit.m b/BNHtmlPdfKit.m index 1a77a8d..f751d11 100644 --- a/BNHtmlPdfKit.m +++ b/BNHtmlPdfKit.m @@ -38,7 +38,7 @@ - (CGRect)printableRect { #pragma mark - BNHtmlPdfKit Extension -@interface BNHtmlPdfKit () +@interface BNHtmlPdfKit () - (CGSize)_sizeFromPageSize:(BNPageSize)pageSize; @@ -46,7 +46,7 @@ - (void)_timeout; - (void)_savePdf; @property (nonatomic, copy) NSString *outputFile; -@property (nonatomic, strong) UIWebView *webView; +@property (nonatomic, strong) WKWebView *webView; @property (nonatomic, copy) void (^dataCompletionBlock)(NSData *pdfData); @property (nonatomic, copy) void (^fileCompletionBlock)(NSString *pdfFileName); @@ -224,7 +224,7 @@ - (id)initWithCustomPageSize:(CGSize)pageSize { - (void)dealloc { [[self class] cancelPreviousPerformRequestsWithTarget:self selector:@selector(_timeout) object:nil]; - [self.webView setDelegate:nil]; + [self.webView setNavigationDelegate:nil]; [self.webView stopLoading]; } @@ -359,8 +359,8 @@ - (void)saveHtmlAsPdf:(NSString *)html { - (void)saveHtmlAsPdf:(NSString *)html toFile:(NSString *)file { self.outputFile = file; - self.webView = [[UIWebView alloc] init]; - self.webView.delegate = self; + self.webView = [[WKWebView alloc] init]; + self.webView.navigationDelegate = self; if (!self.baseUrl) { [self.webView loadHTMLString:html baseURL:[NSURL URLWithString:@"http://localhost"]]; @@ -376,46 +376,47 @@ - (void)saveUrlAsPdf:(NSURL *)url { - (void)saveUrlAsPdf:(NSURL *)url toFile:(NSString *)file { self.outputFile = file; - self.webView = [[UIWebView alloc] init]; - self.webView.delegate = self; + self.webView = [[WKWebView alloc] init]; + self.webView.navigationDelegate = self; - if ([self.webView respondsToSelector:@selector(setSuppressesIncrementalRendering:)]) { - [self.webView setSuppressesIncrementalRendering:YES]; - } + self.webView.configuration.suppressesIncrementalRendering = YES; [self.webView loadRequest:[NSURLRequest requestWithURL:url]]; } -- (void)saveWebViewAsPdf:(UIWebView *)webView { +- (void)saveWebViewAsPdf:(WKWebView *)webView { [self saveWebViewAsPdf:webView toFile:nil]; } -- (void)saveWebViewAsPdf:(UIWebView *)webView toFile:(NSString *)file { +- (void)saveWebViewAsPdf:(WKWebView *)webView toFile:(NSString *)file { [[self class] cancelPreviousPerformRequestsWithTarget:self selector:@selector(_timeout) object:nil]; self.outputFile = file; - webView.delegate = self; + webView.navigationDelegate = self; self.webView = webView; } -#pragma mark - UIWebViewDelegate +#pragma mark - WkNavigationDelegate -- (void)webViewDidFinishLoad:(UIWebView *)webView { - NSString *readyState = [webView stringByEvaluatingJavaScriptFromString:@"document.readyState"]; - BOOL complete = [readyState isEqualToString:@"complete"]; +- (void)webView:(WKWebView *)webView didFinishNavigation:(WKNavigation *)navigation { + [webView evaluateJavaScript:@"document.readyState" completionHandler:^(id _Nullable readyState, NSError * _Nullable error) { + BOOL complete = [readyState isEqualToString:@"complete"]; - [[self class] cancelPreviousPerformRequestsWithTarget:self selector:@selector(_timeout) object:nil]; + [[self class] cancelPreviousPerformRequestsWithTarget:self selector:@selector(_timeout) object:nil]; - if (complete) { - [self _savePdf]; - } else { - [self performSelector:@selector(_timeout) withObject:nil afterDelay:1.0f]; - } + if (complete) { + [self _savePdf]; + } else { + [self performSelector:@selector(_timeout) withObject:nil afterDelay:1.0f]; + } + + }]; } -- (void)webView:(UIWebView *)webView didFailLoadWithError:(NSError *)error { + +- (void)webView:(UIWebView *)webView didFailNavigation:(null_unspecified WKNavigation *)navigation withError:(nonnull NSError *)error { [[self class] cancelPreviousPerformRequestsWithTarget:self selector:@selector(_timeout) object:nil]; if (self.failureBlock) {