2010-08-16 54 views
1

我需要動態修改我的UIWebView中的Javascript。我在我的應用程序的Documents文件夾中本地加載文件html。如何以編程方式修改UIWebView中的JavaScript?

我需要創建一個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的我已保存修改其值..

我怎樣才能做到這一點?

回答

1

首先,你可以從普通的NSData對象加載你的html數據。牢記這一點,你可以從你的文件中加載一個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"]; 

除此之外,你可以考慮一下:

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

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

正如你所看到的,這種方式更爲複雜。

+0

非常感謝:) – 2010-08-17 08:29:38

相關問題