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.
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.
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.
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.
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.
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...
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
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With