UIWebView JavaScript注入 [英] UIWebView JavaScript Injection

查看:65
本文介绍了UIWebView JavaScript注入的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我看了足够多的博客,所以我很确定它的编码方式。但是,似乎没有人提到Obj-C代码中javascript注入的位置吗?基本上,我想做的就是在用户可以浏览的所有页面上(或至少是最初加载的页面上)都删除标题,如果有问题,我将在稍后担心后续页面。愚蠢的)。这是我的代码:

I've been looking at enough blogs so I'm pretty sure how it's coded... but none of them seem to mention where the javascript injection goes in the Obj-C code? Basically all I want to do is get rid of a header on all the pages the user can navigate to (or at least the page that initially loads... I'll worry about subsequent pages later if it's an issue. keep it simple, stupid ). Here's the code I have:

[self stringByEvaluatingJavaScriptFromString:@ document。getElementById('login')。childNodes [1] .innerHTML =]];

[self stringByEvaluatingJavaScriptFromString:@"document. getElementById('login').childNodes[1].innerHTML=''"];

我只是在寻找一个应该真正起作用的地方而已。我将其主要归因于不完全了解UIWebView。我以为我会在这里发布它,然后与此同时阅读它是如何工作的,或者自己弄清楚。

I'm just having a hell of a time finding a place where it should actually work. I attribute it mostly to not fully understanding UIWebView. I figured I'd post this here and then in the meantime read up on how that works and maybe figure it out on my own.

推荐答案

您的javascript代码应在整个HTML文档(及其所有DOM对象)已加载后执行。

Your javascript code should be executed one your whole HTML document has been loaded (and all its DOM objects).

因此,您应该粘贴-(void)webViewDidFinishLoad:(UIWebView *)webView 中的> stringByEvaluatingJavascriptFromString 调用(当然,您需要在对象,该对象将被设置为UIWebView的委托,对于任何委托方法以及每次使用委托模式都将被设置)

Thus you should paste your stringByEvaluatingJavascriptFromString call in the UIWebView's delegate method - (void)webViewDidFinishLoad:(UIWebView *)webView (of course you need to implement this code in an object that will be set as the delegate of the UIWebView, as for any delegate method and for every time you use the delegate pattern)

这篇关于UIWebView JavaScript注入的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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