Inside (BOOL) webView: how to open url clicked in UIWebView that opens Modal UIWebView

Thanks for looking at my question. Let me first describe the application. I have a tab bar based app for iOS5.1 using storyboard and ARC. Each has four tabs with a view controller that shows the webview with local HTML files (each view is a different set of rendered html files).

Currently, when a user taps a http or https link, it will open in Safari. However, now I want it to open in the application via a modal view. I currently have an interface created and ready to show, but I am unable to pass the url to the modal "webview2". I have searched lots of examples for this using RSURL and strings etc, but cannot get them to work. Some of the code examples provided didn't seem to work for my situation.

In my FirstViewController.m file (which is similar to other views), I ...

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

    //Gets the link.
    if (navigationType == UIWebViewNavigationTypeLinkClicked) {
        NSURL *URL = [request URL]; 
        NSLog(@"url:%@",request); //Get the url itself

        //If it an external link...
        if ([[URL scheme] isEqualToString:@"http"] || 
            [[URL scheme] isEqualToString: @"https" ])  {
            [[UIApplication sharedApplication] openURL:[request URL]];
            NSLog(@"Opened in Safari");
            return NO;
        }

        //If it an email address...
        else if ([[URL scheme] isEqualToString:@"mailto"]) {
            [webView loadRequest:request];
            [[UIApplication sharedApplication] openURL:[request URL]];
            NSLog(@"Opened in Mail App");
            return NO;
        }
    }        
    return YES;
}  

      

Which works if I want the url to open in Safari. So in this part ...

//If it an external link...
    if ([[URL scheme] isEqualToString:@"http"] || 
        [[URL scheme] isEqualToString: @"https" ])  {
        [[UIApplication sharedApplication] openURL:[request URL]];
        NSLog(@"Opened in Safari");
        return NO;
    }

      

I need this to open a modal view with a clicked url, not Safari.

So my question is ... What is the best way to get this URL and then open a modal view and then open the URL that was just touched in the modal web browser?

I already have a modal view created with a Cancel button, etc. I just need to load the clicked url when it loads.

I can load the modal via string ...

[self performSegueWithIdentifier:@"ModalWebView" sender:self];

      

but this just loads it without any url information and an empty webview of course. Modal uses WebViewController.h / m.

If anyone can tell me how to open and pass the url so that it loads in the "ModalWebView" segment, I would appreciate it. Please let me know if I need to create any properties or anything else that I need to include in my WebViewController.h / m that launches the modal view. Maybe - (void) prepareForSegue should be used? If so, how?

I realize the answer may be simple, but I'm still a beginner and have spent days trying to find a clear answer. I found answers to many of my questions on this site before searching, but I'm afraid I'm stuck on this one.

Thanks for your time and patience.

+3


source to share


1 answer


In your view controller, implement the method prepareForSegue

and add the following code.

-(void) prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {

    NSLog(@"Source Controller = %@", [segue sourceViewController]);
    NSLog(@"Destination Controller = %@", [segue destinationViewController]);
    NSLog(@"Segue Identifier = %@", [segue identifier]);

    if ([segue.identifier isEqualToString:@"ModalWebView"]) {

        ModalWebViewController *wVC = [segue destinationViewController];
        wVC.url = [NSURL URLWithString:article.url]; // replace article.url with your url.
    }

}

      

In my code above, the url to be used is set from my article.url object. To get this to work, you need to get the url from the method shouldStartLoadWithRequest

.

// CurrentViewController.h
@property (strong, nonatomic) NSURL *targetUrl;

// CurrentViewController.m 
//If it an external link...
    if ([[URL scheme] isEqualToString:@"http"] || 
        [[URL scheme] isEqualToString: @"https" ])  {
        targetUrl = url;
        [self performSegueWithIdentifier:@"ModalWebView" sender:self];
        return NO;
    }

      

If you are using the above, we will now replace article.url in the first block of code with targetUrl.

if ([segue.identifier isEqualToString:@"ModalWebView"]) {

            ModalWebViewController *wVC = [segue destinationViewController];
            wVC.url = targetUrl; 
        }

      



In your view, the controller for your modal view.h file creates an NSURL file.

//ModalWebViewController.h
@property (strong, nonatomic) NSURL *url;

      

In your implementation file (.m) create a method viewDidAppear

//ModalWebViewController.m 
-(void) viewDidAppear:(BOOL)animated {
    [_webView loadRequest:[NSURLRequest requestWithURL:url]];
}

      

I hope I have explained that it is enough for you to implement.

+1


source







All Articles