How to cancel a UIWebView?

倾然丶 夕夏残阳落幕 提交于 2019-12-03 00:30:27
Rob Napier

You're on the right path, but you're approach is slightly off. You don't need or want this to be an HTTP URL. Make your URL cancel:.

<a href="cancel:">Thing to click</a>

Then implement webView:shouldStartLoadWithRequest:navigationType: in your web view delegate. Something like this (untested):

- (BOOL)webView:(UIWebView *)webView shouldStartLoadWithRequest:(NSURLRequest *)request navigationType:(UIWebViewNavigationType)navigationType {
    if ([request.URL.scheme isEqualToString:@"cancel"]) {
        [self dismissModalViewControllerAnimated:YES];
        return NO;
    }
    return YES;
}

It's not entirely clear if you want to stop loading in a web view or just dismiss the modal view controller that's containing it.

To stop loading:

[webView stopLoading];

To dismiss the view controller:

[self dismissModalViewControllerAnimated:YES];

Don't forget to set the web view's delegate to nil before you release it.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!