2011-05-09 3 views
5

안녕하세요! 의 내용을 읽고Xcode webview에서 웹 페이지 대신 로컬 HTML 파일을로드하려면 어떻게해야합니까?

- (void)loadAboutHTML { 
UIWebView *aboutHTML = [[UIWebView alloc] init]; 
NSURL *webURL = [NSURL URLWithString:@"http://apple.com"]; 
NSURLRequest *webURLRequest = [NSURLRequest requestWithURL:webURL]; 
[aboutHTML loadRequest:webURLRequest]; 
[aboutHTML setScalesPageToFit:YES]; 
[aboutHTML setFrame:CGRectMake(0, 0, 320, 416)]; 
[self addSubview:aboutHTML];} 
+2

정답을 받아 들여야합니다. – elp

+0

@elpsk 모든 유용한 것들을 upvoted, 그들 모두를 기반으로 내 자신의 답변을 게시 –

+3

질문에 대답하는 사람들은 자신의 대답에 자신의 질문에 대답하기 위해 upvote에 disservice합니까? 아마 최선의 방법은 아니라고 말하는 것입니다. –

답변

5

옵션 1

사용이

- (void)loadHTMLString:(NSString *)string baseURL:(NSURL *)baseURL 

: 어떻게 대신 코드에서 웹 페이지의 프로젝트에 저장된 로컬 HTML 파일을로드 할 수 있습니다 파일을 프로젝트의 NSString에 저장하십시오. 그런 다음 파일에서 문자열을 확보 한 후 사용하는 HTML 콘텐츠

에게

+ (id)stringWithContentsOfFile:(NSString *)path encoding:(NSStringEncoding)enc error:(NSError **)error 

사용

을로드 위의 방법을 사용

[webView loadHTMLString:urHTMLString baseURL:baseURL]; 

옵션 2

NSURLRequest *urlReq = [NSURLRequest requestWithURL:[NSURL fileURLWithPath:[[NSBundle mainBundle] pathForResource:@"fileName" ofType:@"html"]]]; 
[webView loadRequest:urlReq]; 

업데이트

- (void)loadAboutHTML { 
UIWebView *aboutHTML = [[UIWebView alloc] init]; 
NSURLRequest *urlRequest = [NSURLRequest requestWithURL:[NSURL fileURLWithPath:[[NSBundle mainBundle] pathForResource:@"yourFileName" ofType:@"html"]]]; 
[aboutHTML loadRequest:urlRequest; 
[self addSubview:aboutHTML]; 
} 
+0

@problem child 안녕 얘들 아! 매우 대단히 감사합니다. 그러나이 방법들 중 아무 것도 나를 위해 일하지 않았습니다 :/ 아마 내가 작성한 코드를 취해 그것을 어떻게 수정했는지 보여주기 위해 수정할 수 있습니까? –

+1

html 파일을 프로젝트 리소스 번들에 추가하고이 코드를 사용하십시오. – visakh7

13
UIWebView *web = [[UIWebView alloc] initWithFrame:CGRectMake(0, 0, 320, 460)]; 
[web loadRequest:[NSURLRequest requestWithURL:[NSURL fileURLWithPath:[[NSBundle mainBundle] 
          pathForResource:@"help" ofType:@"html"]isDirectory:NO]]]; 
web.backgroundColor = [UIColor clearColor]; 

그게 전부 와트 내가 사용했다

2

로컬 웹 HTML

[webView loadRequest:[NSURLRequest requestWithURL:[NSURL fileURLWithPath:[[NSBundle mainBundle]                   pathForResource:@"file_name_html" ofType:@"html"] isDirectory:NO]]]; 

HTTP 웹

[webView loadRequest:[NSURLRequest requestWithURL:[NSURL URLWithString:@"http://google.com"]]]; 
2
may be the answer of the solution 

CGRect applicationFrame = [[UIScreen mainScreen] applicationFrame]; 
    applicationFrame.origin.y = 0; 
    webView = [[UIWebView alloc] initWithFrame:applicationFrame]; 
    webView.autoresizingMask = (UIViewAutoresizingFlexibleWidth|UIViewAutoresizingFlexibleHeight); 

    NSString *basePath = [[NSBundle mainBundle] bundlePath]; 
    NSURL *baseURL = [NSURL fileURLWithPath:basePath]; 
    NSString *filePath = [[NSBundle mainBundle] pathForResource:@"index" ofType:@"html"]; 
    NSString *htmlString = [NSString stringWithContentsOfFile:filePath]; 
    if (htmlString) { 
     [webView loadHTMLString:htmlString baseURL:baseURL]; 
    } 

    [self.view addSubview:webView]; 
0
UIWebView *agreementView = [[UIWebView alloc] initWithFrame:CGRectMake(10, newY, 494, 300)]; 
     agreementView.delegate = self; 
     agreementView.dataDetectorTypes = UIDataDetectorTypeNone; 
     [agreementView loadRequest:[NSURLRequest requestWithURL:[NSURL fileURLWithPath:[[NSBundle mainBundle] pathForResource:@"yourfilename" ofType:@"html"]]]]; 
     loadingIndicator=[[UIActivityIndicatorView alloc]initWithActivityIndicatorStyle:UIActivityIndicatorViewStyleGray]; 
     loadingIndicator.frame=CGRectMake(237, 140, 20, 20); 
     loadingIndicator.transform = CGAffineTransformMakeScale(1.55, 1.55); 
     [agreementView addSubview:loadingIndicator]; 
- (void)webViewDidStartLoad:(UIWebView *)webView 
{ 
    loadingIndicator.hidden=NO; 
    [loadingIndicator startAnimating]; 

} 
//delegates 
- (void)webViewDidFinishLoad:(UIWebView *)webView 
{ 
    loadingIndicator.hidden=YES; 
    [loadingIndicator stopAnimating]; 

} 

- (void)webView:(UIWebView *)webView didFailLoadWithError:(NSError *)error 
{ 
    [loadingIndicator stopAnimating]; 
} 

- (BOOL)webView:(UIWebView *)webView shouldStartLoadWithRequest:(NSURLRequest *)request navigationType:(UIWebViewNavigationType)navigationType 
{ 


    if(UIWebViewNavigationTypeOther == navigationType) 
    { 
    return YES; 
    } 
    return NO; 
} 
관련 문제