Search code examples
objective-ciosuiwebviewuiwebviewdelegate

Cancel button/touch for UIWebView


I wonderd if there is any way to make the user dismiss the WebView window if he doesn't want it anymore on the screen...? I looked at this post but i didn't understand it well. How to cancel a UIWebView? can anyone give me an example please? This is the code i have:

CGSize webScreen1;
webScreen1 = [[UIScreen mainScreen] applicationFrame].size;
CGRect webFrame1 = CGRectMake((webScreen1.width/11.0) ,(webScreen1.height/19.0)  ,webScreen1.width/1.2,webScreen1.height/1.25);
defaultWebView.frame = webFrame1;
self.defaultWebView = [[UIWebView alloc] initWithFrame:webFrame1];
self.defaultWebView.backgroundColor = [UIColor whiteColor];
self.defaultWebView.scalesPageToFit = YES;
self.defaultWebView.autoresizingMask = (UIViewAutoresizingFlexibleWidth | UIViewAutoresizingFlexibleHeight);

defaultWebView.inputView.hidden = YES;

[self.view addSubview: self.defaultWebView];

[self.defaultWebView loadRequest:[NSURLRequest requestWithURL:[NSURL URLWithString:
                                                            [NSString stringWithFormat:(NSString *)@"%@", @"http://www.google.com"]]]];

Thanks!


Solution

  • There is no such thing as cancel a webView, what you need to do is remove the UIWebView from the parent view. If your UIWebView is a subview of self.view then you can provide a button named Close which behaves like this -

    - (IBAction)closeWebView:(id)sender
    {
        [self.webView removeFromSuperView];
        self.webView = nil;
        return;
    }
    

    This should remove the webview from your view.