Within a (BOOL)webView: How to open a URL clicked

2019-05-11 21:31发布

Thanks for viewing my question. Let me describe the app first. I have a tab bar based app for iOS5.1 that is using storyboard and ARC. There are four tabs with a view controller in each one that shows a webview with local HTML files (each view is a different set of html files to display).

Currently when a user touches a http or https link, it'll open in Safari. However, I now want it to open within app via a modal view. I currently have the interface created and ready to show, but I can't pass the URL to the modal's "webview2". I've looked for many examples to do this using RSURL and strings and etc, but can't get any of them to work. Some of the example code given didn't seem to work for my situation.

In my FirstViewController.m file (which is same in the other views), I have...

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

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

        //If it's 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's 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 fine if I want the URL to open in Safari. So in this part...

//If it's 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 it to open a modal view with the clicked URL, not Safari.

So what I'm asking is... What is the best way to get this URL saved, then open the modal view and then in the modal webview open the URL that was just touched?

I already have the modal view made with a dismiss button and etc. I just need it to load with the clicked URL when it loads.

I can load the modal via the line...

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

but that just loads it without any URL info and a blank webview, of course. The modal uses WebViewController.h/m.

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

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

Thank you for your time and patience.

1条回答
淡お忘
2楼-- · 2019-05-11 22:01

In your view controller, implement the prepareForSegue method 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 that will 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's an external link...
    if ([[URL scheme] isEqualToString:@"http"] || 
        [[URL scheme] isEqualToString: @"https" ])  {
        targetUrl = url;
        [self performSegueWithIdentifier:@"ModalWebView" sender:self];
        return NO;
    }

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

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

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

In your view controller for your modal web view.h file create a NSURL.

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

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

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

Hope I've explained that enough for you to be able to implement.

查看更多
登录 后发表回答