How to get JSContext from WKWebView

You cannot obtain the context, because layout and javascript is handled on another process. Instead, add scripts to your webview configuration, and set your view controller (or another object) as the script message handler. Now, send messages from JavaScript like so: window.webkit.messageHandlers.interOp.postMessage(message) Your script message handler will receive a callback: – (void)userContentController:(WKUserContentController *)userContentController didReceiveScriptMessage:(WKScriptMessage *)message{ … Read more

How to safely shut down a loading UIWebView in viewWillDisappear?

A variation on this should fix both the leaking and zombie issues: – (void)loadRequest:(NSURLRequest *)request { [self retain]; if ([webView isLoading]) [webView stopLoading]; [webView loadRequest:request]; [self release]; } – (void)webViewDidStartLoad:(UIWebView *)webView { [self retain]; } – (void)webViewDidFinishLoad:(UIWebView *)webView { [self release]; } – (void)webView:(UIWebView *)webView didFailLoadWithError:(NSError *)error { [self release]; } – (void)viewWillDisappear { if … Read more

How to get HTTP headers

This falls under the easy, but not obvious class of iPhone programming problems. Worthy of a quick post: The headers for an HTTP connection are included in the NSHTTPURLResponse class. If you have an NSHTTPURLResponse variable you can easily get the headers out as a NSDictionary by sending the allHeaderFields message. For synchronous requests — … Read more

Remove UIWebView Shadow?

This is a cleaner alternative to “Nikolai Krill” solution. This only hides UIImageViews within the UIWebView and not the UIWebBrowserView. for (UIView *view in [[[webView subviews] objectAtIndex:0] subviews]) { if ([view isKindOfClass:[UIImageView class]]) view.hidden = YES; } Thanks James

UIWebView Link Click

Use the Delegate to Determine the Navigation Type! My Snippet – (BOOL)webView:(UIWebView *)webView shouldStartLoadWithRequest:(NSURLRequest *)request navigationType:(UIWebViewNavigationType)navigationType{ if (navigationType == UIWebViewNavigationTypeLinkClicked){ NSURL *url = request.URL; [self openExternalURL:url];//Handle External URL here } return YES; }