Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to cancel a UIWebView?

I'm modally 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"?

like image 947
ma11hew28 Avatar asked Aug 18 '11 15:08

ma11hew28


1 Answers

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;
}
like image 171
Rob Napier Avatar answered Oct 22 '22 03:10

Rob Napier