[英]How to modify JavaScript in a UIWebView programmatically?
I need to modify dynamically a Javascript inside my UIWebView. 我需要在UIWebView中动态修改Javascript。 I load a file html locally, inside the Documents folder of my app.
我在应用程序的Documents文件夹内本地加载html文件。
I need to create a var attributes like this: 我需要创建一个这样的var属性:
var attributes = {
"feedURL": "http://www.obliviux.com/?feed=rss2", //insert NSString *RSS here
"maxAgeToShow": "-1", //insert NSString *maxAge here
"numItemsToShow": "10", //insert NSString *numItems here
"topStories": "3" //insert NSString *topStories here
};
And modify their value with some NSString I have saved.. 并用我保存的一些NSString修改它们的值。
How can i do this? 我怎样才能做到这一点?
First of all, you can load your html-data from a plain NSData-object. 首先,您可以从普通的NSData对象加载html数据。 Keeping that in mind, you can load a NSString from your file, replacing '__feed_url__' (for example) with your NSString.
请记住,您可以从文件中加载NSString,例如用NSString替换'__feed_url__'。
UIWebView *webView = [[UIWebView alloc] initWithFrame:yourRect];
NSString *template = [[NSString alloc] initWithContentsOfFile:yourTemplateFile];
template = [template stringByReplacingOccuresOfString:@"__feed_url__" withString:yourFeedURL];
[webView loadData:[template dataUsingEncoding:NSUTF8StringEncoding] MIMEType:@"text/html" textEncodingName:@"utf8"];
Besides that, you could consider this: 除此之外,您可以考虑以下几点:
- (void)viewDidLoad {
...
self.webView.delegate = self;
...
}
- (void)webViewDidFinishLoad:(UIWebView *)someWebView {
NSString *result = [self.webView stringByEvaluatingJavaScriptFromString:[NSString stringWithFormat:@"attributes['feedURL'] = '%@'", yourFeedURL]];
}
As you can see, this way is far more complicated. 如您所见,这种方式要复杂得多。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.