2011-05-09 92 views
5

嘿! 我怎樣才能加載保存的項目,而不是在這個代碼網頁中的本地HTML文件:如何在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 2012-12-18 18:46:16

+0

@elpsk upvoted所有有幫助的,根據他們發佈我自己的答案 – 2012-12-26 16:59:51

+3

回答這個問題的人有什麼不喜歡upvote以回答你自己的問題與他們的迴應嗎?只是說這可能不是最佳做法。 – 2013-04-03 20:22:43

回答

5

OPTION 1

使用此

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

閱讀的內容將您項目中的文件轉換爲NSString。然後使用上面的方法加載的HTML內容

使用

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

以獲得來自文件中的字符串,然後使用

[webView loadHTMLString:urHTMLString baseURL:baseURL]; 

OPTION 2

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

UPDATE

- (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

@問題孩子 嘿傢伙!非常感謝你,但是這些方法都不適用於我:/ 你可能會把我寫在那裏的代碼修改成向我展示它是如何完成的? – 2011-05-10 11:02:05

+1

將您的html文件添加到項目資源包並使用此代碼。 – visakh7 2011-05-10 11:08:52

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; 
} 
相關問題