2010-08-16 3 views
1

UIWebView 내에서 동적으로 자바 스크립트를 수정해야합니다. 내 응용 프로그램의 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을 '__feed_url__'(예 :) 대신 NSString을 파일에서로드 할 수 있습니다.

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

고맙습니다. :) –

관련 문제