Question

I am building an iOS application that uses the SWRevealViewController to display slide-out menus.

In my slide out menu, I have a WebView which contains some links. When a user clicks the link, I want the SWRevealViewController to slide back (I can do that easy enough), and the URL opens in the MainViewController WebView (not so easy for me).

I want to make sure, if possible, that the URL doesn't change at all in the SWRevealViewController. Just starts the action to open the URL in MainViewController.

How can I achieve this?

Was it helpful?

Solution

  1. Add UIWebViewDelegate as an protocol your view controller implements.
  2. Set yourself as the delegate to the UIWebView
  3. Add the method - (BOOL)webView:(UIWebView *)webView shouldStartLoadWithRequest:(NSURLRequest *)request navigationType:(UIWebViewNavigationType)navigationType to your view controller.
  4. Return NO from this method (unless it is for the initial page load) and capture the URL to navigate to in another view controller.

In your header add the UIWebViewDelegate protocol

@interface MyViewController : UIViewController <UIWebViewDelegate>

In your viewDidLoad set the delegate (Assuming you have an @property for the webView with an IBOutlet set to the UIWebView)

self.webView.delegate = self;

Then add the delegate method for loading requests.

- (BOOL)webView:(UIWebView*)webView shouldStartLoadWithRequest:(NSURLRequest*)request navigationType:(UIWebViewNavigationType)navigationType
{
    NSString* urlStr = [[request URL] absoluteString];
    if(request == nil || [urlStr isEqualToString:@"http://YourInitialURL"])
    {
        return YES;
    }

    //Use this url to change the other web view in your main view controller
    NSURL* url = [request URL];

    return NO;
}
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top