Skip to content

Instantly share code, notes, and snippets.

@ldenoue
Created November 8, 2019 13:11
Show Gist options
  • Save ldenoue/d7ac4a748bdac6234487169f0e23766e to your computer and use it in GitHub Desktop.
Save ldenoue/d7ac4a748bdac6234487169f0e23766e to your computer and use it in GitHub Desktop.
Capture and share the full content of a WKWebView as a UIImage
-(void)shareHighlightsAsImage:(NSDictionary *)note
{
NSString *filePath = [[NSBundle mainBundle] pathForResource:@"quotes" ofType:@"html"];
NSString *fileContents = [NSString stringWithContentsOfFile: filePath encoding:NSUTF8StringEncoding error:nil];
fileContents = [fileContents stringByReplacingOccurrencesOfString:@"{{url}}" withString:note[@"url"]];
fileContents = [fileContents stringByReplacingOccurrencesOfString:@"{{title}}" withString:note[@"title"]];
fileContents = [fileContents stringByReplacingOccurrencesOfString:@"{{datetime}}" withString:note[@"datetime"]];
fileContents = [fileContents stringByReplacingOccurrencesOfString:@"{{highlights}}" withString:note[@"highlights"]];
WKWebViewConfiguration *config = [[WKWebViewConfiguration alloc] init];
WKWebView *web = [[WKWebView alloc] initWithFrame:CGRectMake(self.view.frame.size.width, 0, 420, 480) configuration:config];
web.navigationDelegate = self;
[web loadHTMLString:fileContents baseURL:nil];
[self.view addSubview:web];
}
- (void) webView:(WKWebView *) webView decidePolicyForNavigationAction:(WKNavigationAction *) navigationAction decisionHandler:(void (^)(WKNavigationActionPolicy)) decisionHandler {
decisionHandler(WKNavigationActionPolicyAllow);
}
- (void)webView:(WKWebView *)webView didFinishNavigation:(null_unspecified WKNavigation *)navigation
{
[self performSelector:@selector(shareAsImage:) withObject:webView afterDelay:0.1];
}
-(void)shareAsImage:(WKWebView *)webView
{
WKSnapshotConfiguration *configuration = [[WKSnapshotConfiguration alloc] init];
configuration.rect = CGRectMake(0, 0, webView.scrollView.contentSize.width, webView.scrollView.contentSize.height);
webView.frame = CGRectMake(self.view.frame.size.width, 0, webView.scrollView.contentSize.width, webView.scrollView.contentSize.height);
dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(1.0 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
[webView takeSnapshotWithConfiguration:configuration completionHandler:^(UIImage *snapshotImage, NSError *error) {
[webView removeFromSuperview];
NSArray *items = @[snapshotImage];
UIActivityViewController *controller = [[UIActivityViewController alloc] initWithActivityItems:items applicationActivities:nil];
controller.excludedActivityTypes = @[UIActivityTypeCopyToPasteboard,UIActivityTypeAddToReadingList];
//[controller setValue:title forKey:@"subject"];
[controller setCompletionWithItemsHandler:^(NSString *activityType,
BOOL completed,
NSArray *returnedObjects,
NSError *error){
[self doneWithResults:@{}];
}];
UIPopoverPresentationController *presentationController =
[controller popoverPresentationController];
presentationController.sourceView = self.view;
[self presentViewController:controller animated:YES completion:nil];
}];
});
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment