如何以编程方式修改UIWebView中的JavaScript?

如何以编程方式修改UIWebView中的JavaScript?,javascript,xcode,uiwebview,Javascript,Xcode,Uiwebview,我需要在UIWebView中动态修改Javascript。我在本地加载了一个html文件,在我的应用程序的Documents文件夹中 我需要创建一个var属性,如下所示: var attributes = { "feedURL": "http://www.obliviux.com/?feed=rss2", //insert NSString *RSS here "maxAgeToShow": "-1", //insert NSString *maxAge here "n

我需要在UIWebView中动态修改Javascript。我在本地加载了一个html文件,在我的应用程序的Documents文件夹中

我需要创建一个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
};
并用我保存的NSString修改它们的值


如何实现这一点?

首先,您可以从普通NSData对象加载html数据。记住这一点,您可以从文件中加载NSString,例如用NSString替换“\uuuuu feed\uu 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"];

除此之外,你可以考虑:

- (void)viewDidLoad {
    ...
    self.webView.delegate = self;
    ...
}

- (void)webViewDidFinishLoad:(UIWebView *)someWebView {
    NSString *result = [self.webView stringByEvaluatingJavaScriptFromString:[NSString stringWithFormat:@"attributes['feedURL'] = '%@'", yourFeedURL]];
}

如您所见,这种方式要复杂得多。

首先,您可以从普通的NSData对象加载html数据。记住这一点,您可以从文件中加载NSString,例如用NSString替换“\uuuuu feed\uu 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"];

除此之外,你可以考虑:

- (void)viewDidLoad {
    ...
    self.webView.delegate = self;
    ...
}

- (void)webViewDidFinishLoad:(UIWebView *)someWebView {
    NSString *result = [self.webView stringByEvaluatingJavaScriptFromString:[NSString stringWithFormat:@"attributes['feedURL'] = '%@'", yourFeedURL]];
}
正如你所看到的,这种方式要复杂得多