Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Use WKWebKit instead of UIWebView #23

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 3 additions & 2 deletions BNHtmlPdfKit.h
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
//

#import <Foundation/Foundation.h>
#import <WebKit/WebKit.h>

@protocol BNHtmlPdfKitDelegate;

Expand Down Expand Up @@ -318,15 +319,15 @@ 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.

@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.
Expand Down
49 changes: 25 additions & 24 deletions BNHtmlPdfKit.m
Original file line number Diff line number Diff line change
Expand Up @@ -38,15 +38,15 @@ - (CGRect)printableRect {

#pragma mark - BNHtmlPdfKit Extension

@interface BNHtmlPdfKit () <UIWebViewDelegate>
@interface BNHtmlPdfKit () <WKNavigationDelegate>

- (CGSize)_sizeFromPageSize:(BNPageSize)pageSize;

- (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);
Expand Down Expand Up @@ -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];
}

Expand Down Expand Up @@ -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"]];
Expand All @@ -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) {
Expand Down