Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Get notified about a page change in UIWebView

I'm curious if there's a way to detect a change of the displayed website inside a UIWebView. In an app I'm working on we're using a UIWebView to present a part of the functionality which are basically a few steps the user needs to follow. However, the will be a specific page at the end of these steps that I need to identify and after which the app needs to display a different view.

Unfortunately the UIWebViewDelegate doesn't seem to do the trick.

like image 657
flohei Avatar asked May 14 '10 14:05

flohei


People also ask

Is UIWebView deprecated?

Apple is phasing out UIWebView, which is used by developers for integrating web content into an app in a quick and secure manner. Apple is replacing UIWebView (and WebView) with WKWebView, an updated version, as UIWebView has been deprecated.

What is the difference between UIWebView and WKWebView?

Unlike UIWebView, which does not support server authentication challenges, WKWebView does. In practical terms, this means that when using WKWebView, you can enter site credentials for password-protected websites.

Is WKWebView same as Safari?

You can think of WKWebView as a stripped-down version of Safari. It is responsible to load a URL request and display the web content. WKWebView has the benefit of the Nitro JavaScript engine and offers more features. If you just need to display a specific web page, WKWebView is the best option for this scenario.

What is Mobile Safari UI WKWebView?

In the simplest terms, WKWebView allows mobile app developers to integrate mobile web content into their User Interface through the use of web views. The WKWebView API renders a complete mobile browser experience within the app itself allowing users to interact with web content while remaining in the app.


2 Answers

If the user steps are separated on different pages, and the UIWebView needs to load the different steps, this should indeed be possible with the delegate methods.

In this example, we say that the user goes through 3 steps, named step1.htm, step2.htm and finally step3.htm. This code will detect when the user reaches the third and final step.

- (BOOL)webView:(UIWebView *)webView shouldStartLoadWithRequest:(NSURLRequest *)request navigationType:(UIWebViewNavigationType)navigationType {
    NSString *URLString = [[request URL] absoluteString];
    if ([URLString isEqualToString:@"http://www.example.com/step3.htm"]) {
        // The user reached step 3!
    }
    return YES;
}

Note that using the absoluteString method might not be the best way to find out where the user is browsing. Take a look at query, path, parameterString, etc...

like image 119
alleus Avatar answered Oct 21 '22 20:10

alleus


in Swift 4

func webView(_ webView: UIWebView, shouldStartLoadWith request: URLRequest, navigationType: UIWebViewNavigationType) -> Bool {

    if request.url?.absoluteString == "http://www.example.com/step3.htm" {
        // The user reached step 3!
    }

    return true
}
like image 37
Andres Marin Avatar answered Oct 21 '22 20:10

Andres Marin