简体   繁体   English

扩充UIWebView使用的URL的最佳(最安全)方法是什么?

[英]What is the best (safest) way to augment the URL used by UIWebView?

We are developing a hybrid application for iOS. 我们正在为iOS开发混合应用程序。 Most of the application is a WebApp running within a UIWebView. 大多数应用程序是在UIWebView中运行的WebApp。 It is sometimes necessary for the native portion of the application to modify the URL, adding some parameters. 有时,应用程序的本机部分必须修改URL,并添加一些参数。 I'm aware there are "alternate" solutions we could use (eg native app talks to our server independently of the webview); 我知道我们可以使用“替代”解决方案(例如,与Web视图无关的本机应用程序与我们的服务器对话); however, I'm primarily interested in a good & safe solution to URL augmentation. 但是,我主要对URL扩展的良好和安全解决方案感兴趣。

I've been looking for the "best" technique to do this, and found two that appear to (mostly) work. 我一直在寻找实现此目的的“最佳”技术,并发现了两个似乎(主要)有效的方法。

Technique #1 - re-initialize the URL 技术#1-重新初始化URL

- (BOOL)webView:(UIWebView *)theWebView shouldStartLoadWithRequest:(NSURLRequest *)request navigationType:(UIWebViewNavigationType)navigationType
{
    NSLog(@"GOTO: %@", [[request URL] absoluteString]);
    NSURL * oldurl = request.URL;
    [[request URL] initWithString:[NSString stringWithFormat:@"%@&newParam=dave", [oldurl absoluteString]]];
    return YES;
}

Technique #2 - reject the 1st URL, and sub-in a new one 技术#2-拒绝第一个URL,并使用一个新的URL

- (BOOL)webView:(UIWebView *)theWebView shouldStartLoadWithRequest:(NSURLRequest *)request navigationType:(UIWebViewNavigationType)navigationType
{
    NSURL * oldurl = request.URL;
    if ([self isFixed:oldurl]) {
        return YES;
    } else {
        NSURL * newurl = [NSURL URLWithString:[NSString stringWithFormat:@"%@&newParam=dave", [oldurl absoluteString]]];
        [theWebView loadRequest:[NSURLRequest requestWithURL:newurl]];
        return NO;
    }
}

Technique #2 seems to cause artifacts on some pages ("iFrame load interrupted", maybe other issues). 技术#2似乎会导致某些页面出现伪像(“ iFrame加载中断”,也许还有其他问题)。

I'm curious if anyone else has done url replacement like this, or via another technique, or if anyone is is aware of problems with technique #1 (which I'll hopefully use). 我很好奇,是否有人通过这种方法或通过其他技术完成了网址替换,或者是否有人意识到技术#1的问题(我希望我会使用)。

I know method swizzling is not recommended, and Apple's document recommends against subclassing UIWebView. 我知道不建议使用方法混乱,Apple的文档建议不要继承UIWebView。

Thanks! 谢谢!

UPDATE Whoops - technique #1 doesn't actually work. 更新糟糕-技术#1实际上不起作用。 My actual code was doing more than I posted, and it only appeared to work... This is probably good news, since it was a little more evil that I'd like. 我的实际代码做得比我发布的要多,它似乎只能起作用...这可能是个好消息,因为我想它更加邪恶。

Never call an -init method on an object that has already been initialized. 切勿在已初始化的对象上调用-init方法。 This is not defined behavior. 这不是定义的行为。 It is quite likely that you are leaking memory here since the old request ivars are probably not released correctly. 您很可能在这里泄漏内存,因为旧的请求ivars可能未正确释放。

Something similar to the second approach is generally preferred. 通常优选与第二种方法类似的方法。

Another approach is to use NSURLProtocol to inject yourself into the URL loading process. 另一种方法是使用NSURLProtocol将自己注入URL加载过程。 Doing it that way, you can create your own NSURLConnection using a modified NSURLRequest . 这样,您可以使用修改后的NSURLRequest创建自己的NSURLConnection

Here's another way to try out. 这是另一种尝试的方法。 I was playing with the stringByEvaluatingJavaScriptFromString: , and found out I could do this: 我在玩stringByEvaluatingJavaScriptFromString:发现我可以这样做:

- (BOOL)webView:(UIWebView *)theWebView shouldStartLoadWithRequest:(NSURLRequest *)request navigationType:(UIWebViewNavigationType)navigationType
{
    NSString *oldUrlString = [NSString stringWithFormat:"%@", request.URL.absoluteURL];

    if ([oldUrlString isEqualToString:@"http://www.google.com/"])
    {
        [webView stringByEvaluatingJavaScriptFromString:@"window.location = \"http://www.arstechnica.com\""];

        return NO;
    }

    return YES;
}

I managed to redirect using a javascript injection, the first request to www.google.com is rejected, and redirected to www.arstechnica.com (I just picked a random site). 我设法使用JavaScript注入进行重定向,但对www.google.com的第一个请求被拒绝,然后重定向至www.arstechnica.com(我只是选择了一个随机站点)。

I think it's slightly better than hot-swapping the URL on the fly. 我认为这比动态交换URL略好。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM