开发者

How to cancel a UIWebView?

开发者 https://www.devze.com 2023-03-29 15:09 出处:网络
I\'m mo开发者_StackOverflow社区dally presenting a UIViewController with a UIWebView as its view. How do I dismiss the UIWebView when the user hits a cancel button on it?

I'm mo开发者_StackOverflow社区dally presenting a UIViewController with a UIWebView as its view. How do I dismiss the UIWebView when the user hits a cancel button on it?

One idea I have is to have the cancel button link to http://cancel and then check in

- (void)webViewDidStartLoad:(UIWebView *)webView

If webView.request.URL.host isEqualToString:@"cancel", then dismiss the view controller.

Does the host have to have a dot in it, e.g., "cancel.com"?


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.

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号