在(BOOL)webView中:如何打开在UIWebView中单击的URL,该URL打开模态UIWebView [英] Within a (BOOL)webView: How to open a URL clicked in UIWebView that opens a Modal UIWebView

查看:75
本文介绍了在(BOOL)webView中:如何打开在UIWebView中单击的URL,该URL打开模态UIWebView的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

感谢您查看我的问题.首先让我描述一下该应用程序.我有一个用于iOS5.1的基于选项卡的应用程序,正在使用情节提要和ARC.每个视图都有四个选项卡,每个选项卡都有一个视图控制器,用于显示包含本地HTML文件的Web视图(每个视图是要显示的一组不同的html文件).

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).

当前,当用户触摸http或https链接时,它将在Safari中打开.但是,我现在希望它通过模式视图在应用程序中打开.目前,我已经创建了界面并准备显示它,但是我无法将URL传递给模式的"webview2".我已经寻找了许多使用RSURL和字符串等来实现此目的的示例,但是其中的任何一个都无法正常工作.给出的一些示例代码似乎不适用于我的情况.

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.

在我的FirstViewController.m文件(与其他视图相同)中,我有...

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;
}  

如果我希望在Safari中打开URL,那可以很好地工作.所以在这一部分...

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;
    }

我需要它以单击的URL(而不是Safari)打开模式视图.

I need it to open a modal view with the clicked URL, not Safari.

所以我要问的是... 保存此URL,然后打开模式视图,然后在模式Web视图中打开刚刚触摸过的URL的最佳方法是什么?

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?

我已经具有通过关闭按钮等创建的模式视图.我只需要在加载时使用单击的URL来加载它即可.

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];

,但是,它只是加载了它,没有任何URL信息和空白的Webview.该模式使用WebViewController.h/m.

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

如果有人可以告诉我如何打开并传递URL,以便将其加载到segue"ModalWebView"中,我将不胜感激.请让我知道是否需要设置运行模式视图的WebViewController.h/m中包含的任何属性或其他任何属性.也许需要使用-(void)prepareForSegue?如果可以,怎么办?

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.

感谢您的时间和耐心.

推荐答案

在视图控制器中,实现prepareForSegue方法并添加以下代码.

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.
    }

}

在上面的代码中,将使用的URL是从我的article.url对象设置的.为了使它起作用,您需要从方法shouldStartLoadWithRequest中获取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;
    }

如果使用上述代码,我们现在将第一个代码块中的article.url替换为targetUrl.

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; 
        }

在用于模式Web view.h文件的视图控制器中,创建一个NSURL.

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

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

在实现文件(.m)中,创建方法viewDidAppear

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.

这篇关于在(BOOL)webView中:如何打开在UIWebView中单击的URL,该URL打开模态UIWebView的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆