Get notified about a page change in UIWebView

前端 未结 2 1639
长发绾君心
长发绾君心 2020-12-30 06:53

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 t

相关标签:
2条回答
  • 2020-12-30 07:07

    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...

    0 讨论(0)
  • 2020-12-30 07:19

    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
    }
    
    0 讨论(0)
提交回复
热议问题