Search code examples
ioscocoauiviewcontrolleruiwebviewuiwebviewdelegate

Presenting ViewController from UIWebView delegate


I have a UIWebView with a delegate id<UIWebViewDelegate>. When I click on links in the UIWebView, I get the NSURLRequest object in the callback:

 - (BOOL)webView:(UIWebView *)webView shouldStartLoadWithRequest:(NSURLRequest *)request navigationType:(UIWebViewNavigationType)navigationType {

What I'm trying to do is:

- (void)presentWebViewControllerWithUrl:(NSURL*)URL{
      SVModalWebViewController *webViewController = [[SVModalWebViewController alloc] initWithURL:URL];
      webViewController.modalPresentationStyle = UIModalPresentationPageSheet;
      webViewController.availableActions = SVWebViewControllerAvailableActionsOpenInSafari | SVWebViewControllerAvailableActionsCopyLink | SVWebViewControllerAvailableActionsMailLink;
      [self presentModalViewController:webViewController animated:YES];
}

When I place this code in my main view controller, the modal web view appears, but it doesn't if I place it in my delegate. How do I make it appear?


update:

Returning NO from the shouldStartLoadWithRequest simply makes my UIWebViews turn white, with no content. It seems that the callback is being called when the webviews load. I am loading locally stored HTML into them with [web loadHTMLString:@"..." baseUrl: nil] The modal never appears.

My view hierarchy is as follows:

  1. MainView
    • UIView
      • UIScrollView
        • UIWebView
        • UIWebView
        • ...

Is there a way to "bubble" up the callbacks so that I can see the event in my main view controller?


Solution

  • make sure to return (NO) when overriding your shouldStartLoadWithRequest delegate method.

    Also, I don't see the [viewController presentModalViewController:webViewController animated:YES] in your code, something like this:

     - (BOOL)webView:(UIWebView *)webView shouldStartLoadWithRequest:(NSURLRequest *)request navigationType:(UIWebViewNavigationType)navigationType 
        {
         if (navigationType != UIWebViewNavigationTypeLinkClicked)
          return (YES);
    
         SVModalWebViewController *webViewController = [[SVModalWebViewController alloc] initWithURL:[request URL]]; 
         webViewController.modalPresentationStyle = UIModalPresentationPageSheet;  
         webViewController.availableActions = SVWebViewControllerAvailableActionsOpenInSafari | SVWebViewControllerAvailableActionsCopyLink | SVWebViewControllerAvailableActionsMailLink; 
         [myViewController presentModalViewController:webViewController animated:YES];
         return(NO);
        }