2010-03-24 6 views
1

저는 앱용 HTML 파일을 생성하고 있습니다.이 HTML 파일에는 스타일 시트에 대한 링크와 이미지에 대한 링크가 있습니다.생성 된 HTML UIWebView에 이미지 및 CSS 파일 사용

그것은 바로 파일에 대한 권리의 전체 경로를 생성
NSMutableString *toReturn = [NSMutableString stringWithCapacity:100]; 
NSString *cssPath = [[NSBundle mainBundle] pathForResource:@"etapes.css" ofType:nil]; 

[toReturn appendFormat:@"<html><head><title></title><link href=\"%@\" media=\"all\" rel=\"stylesheet\" type=\"text/css\" /></head><body>", cssPath]; 

, 이것이 내가 내 Mac에 액세스하려면 괜찮지 만, 시뮬레이터 또는 내 아이폰 그것에 : 여기

는 지금까지 시도 무엇 올바른 장소를 가리키고 있지 않습니다 ...

내가 어떻게 이걸 만들 수 있니?

감사

답변

4

나는 잠시 뒤로 this tutorial을 발견했다. 결국 < 스타일 > 태그를 사용하여 끝났다.

NSString *path = [[NSBundle mainBundle] bundlePath]; 
NSURL *baseURL = [NSURL fileURLWithPath:path]; 
[webView loadHTMLString:htmlString baseURL:baseURL]; 

이 일을하는 또 다른 방법은 지금과 같다 :

-(NSString *)urlForFileNamed:(NSString *) name ofType:(NSString *) type { 
    NSString *filePath = [[NSBundle mainBundle] pathForResource: name ofType: type]; 
    if (!filePath) { NSLog(@"No path found for file %@.%@", name, type); } 
    return filePath; 
} 

-(void)viewDidLoad { 
    [super viewDidLoad]; 

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

    [view loadHTMLString: [self html] baseURL: [NSURL fileURLWithPath: path]]; 
} 

-(NSString *)html { 
    // Obviously the below's just a stub for proper HTML 
    return [NSString stringWithFormat: @"<img src=\"@\" />", [self urlForFileNamed: @"foo" ofType: @"png"]]; 
} 
+0

이 올바른지에 라인을

NSString *cssPath = [[NSBundle mainBundle] pathForResource:@"etapes.css" ofType:nil]; 

을 편집하려고 할 수 있습니다. 번들에 로컬 URL을 사용하고 HTML에 상대 경로 만 사용하십시오. 스타일 시트의 URL은 간단히'etapes.css'입니다. –

+0

baseURL을 지정해야했습니다! 고마워요. – TomShreds

+0

나를위한 첫 번째 작품. 감사 –

1

당신은

NSString *cssPath = [[NSBundle mainBundle] pathForResource:@"etapes" ofType:@"css"]; 
관련 문제