Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

UIWebView delegate method shouldStartLoadWithRequest: equivalent in WKWebView?

I have a module inside my iOS 7+ app which is a UIWebView. The html page loads a javascript that creates custom-shaped buttons (using the Raphaeljs library). With UIWebView, I set delegate to self. The delegate method webView: shouldStartLoadWithRequest: navigationType: is called each time one of my custom button is pressed. The requests should not be handled by the html, but rather by the iOS code. So I used a request convention (read somewhere here on stackoverflow) using "inapp" as the scheme of my requests. I then check for the host and take the appropriate action.

This code works fine on iOS 7. But the web views appear blank on iOS 8 (bug?), so I decided to use WKWebView for iOS 8 devices. The web views now render fine (and amazingly faster!), but my buttons have no effect.

I tried using - (WKNaviation *)loadRequest:(NSURLRequest *)request, but it's not called.

I can't find a direct equivalent of the UIWebView delegate method webView: shouldStartLoadWithRequest: navigationType:. What's the best way of handling those requests with WKWebView?

like image 796
invalidArgument Avatar asked Sep 30 '14 19:09

invalidArgument


People also ask

What is the difference between UIWebview and WKWebView?

Difference Between UIWebview and WKWebViewUIWebview is a part of UIKit, so it is available to your apps as standard. You don't need to import anything, it will we there by default. But WKWebView is run in a separate process to your app,. You need to import Webkit to use WKWebView in your app.

How can I clear the contents of a UIWebview WKWebView?

To clear old contents of webview With UIWebView you would use UIWebViewDelegate 's - webViewDidFinishLoad: .

What is a WKWebView?

Overview. A WKWebView object is a platform-native view that you use to incorporate web content seamlessly into your app's UI. A web view supports a full web-browsing experience, and presents HTML, CSS, and JavaScript content alongside your app's native views.


2 Answers

I've been looking for a good explanation myself, but haven't found one. I've used the following in my app and everything seems to work (Edit: updated based on ccoroom's comment):

UIWebViewDelegate     - webView:shouldStartLoadWithRequest:navigationType: WKNavigationDelegate  - webView:decidePolicyForNavigationAction:decisionHandler: 

Here's the other UIWebViewDelegate methods:

UIWebViewDelegate     - webViewDidStartLoad: WKNavigationDelegate  - webView:didCommitNavigation:  UIWebViewDelegate     - webViewDidFinishLoad: WKNavigationDelegate  - webView:didFinishNavigation:  UIWebViewDelegate     - webView:didFailLoadWithError: WKNavigationDelegate  - webView:didFailNavigation:withError:                       - webView:didFailProvisionalNavigation:withError: 

I'd love for someone to confirm this for me though.

Edit: Actually, I've answered the question you had in the title (although I'm no longer confident that webView:didCommitNavigation: is called at the exact same point in the lifecycle), but re-reading your description it looks like what you actually need to know about is how to reimplement a Javascript/Objective-C bridge using WKWebView. So have a look at my other answer.

like image 135
SeanR Avatar answered Nov 05 '22 04:11

SeanR


To answer the original question, the equivalent of webView:shouldStartLoadWithRequest:navigationType: in UIWebView is webView:decidePolicyForNavigationAction:decisionHandler: in WKWebView. These methods are called before each request is made (including the initial request) and provide the ability to allow/disallow it.

like image 23
robnasby Avatar answered Nov 05 '22 03:11

robnasby