2009-12-12 3 views
1

나는 로컬 파일을 참조하는 자바 스크립트 실행 stringByEvaluatingJavaScriptFromString를 사용하려면 (이 경우 CSS 파일에,하지만 너무 잠재적 JS 파일) 장치에있는 (문서에는 것 같아요 폴더?) ...로컬 파일을 참조하는 Javascript에서 stringByEvaluatingJavaScriptFromString을 사용할 수 있습니까?

NSString *theJS = [[NSString alloc] 
initWithString:@"javascript:(function() 
{the_css.rel='stylesheet'; 
the_css.href='the_css.css'; 
the_css.type='text/css'; 
the_css.media='screen';} 
)();"]; 

[webView stringByEvaluatingJavaScriptFromString:theJS]; 

어떻게 작동합니까? 내가 외부 CSS 파일을 사용하는 경우는 이것은로 번들의 위치를 ​​전달합니다 나는 HTML

NSString *bundlePath = [[NSBundle mainBundle] bundlePath]; 
NSURL *baseURL = [NSURL fileURLWithPath:bundlePath]; 

[webView loadData:data MIMEType:@"text/html" textEncodingName:@"utf-8 " baseURL:baseURL]; 

를로드 할 때이 코드를 사용 과거에했던 어떤

the_css.href='http://www.website.com/the_css.css'; 

답변

4

처럼 잘 작동 상대 URL이 올바르게 작동하도록하는 기본 URL 물론 정확하게이 loadData 메서드를 사용할 필요는 없으며 다양한 오버로드가 있습니다. 그냥 거기에 baseUrl 얻을 그리고 당신은 좋은 가야한다.

다른 모든 사용자가 같은 것을 사용할 수 있습니다 실패하면 일부 문자열 교체 또는 무엇 이건 페이지에 그

NSString *path = [[NSBundle mainBundle] pathForResource:@"file.css" ofType:nil]] 

을 그리고 주입. 그래도 조금 해키.

0

이렇게해야합니다.

NSString *cssPath = [[[NSBundle mainBundle] resourcePath] stringByAppendingPathComponent:@"the_css.css"]; 
NSURL *baseURL = [NSURL fileURLWithPath:cssPath]; 


NSString *theJS = [[NSString alloc] 
initWithFormat:@"javascript:(function() 
{the_css.rel='stylesheet'; 
the_css.href='%@'; 
the_css.type='text/css'; 
the_css.media='screen';} 
)();",baseURL]; 
[cssPath release]; 

[webView stringByEvaluatingJavaScriptFromString:theJS]; 

희망이 있으면 문제가 해결 될 것입니다.

관련 문제