2011-05-09 5 views
5

Hey! Comment puis-je charger un fichier HTML local enregistré sur le projet au lieu d'une page Web dans ce code:Comment puis-je charger un fichier HTML local au lieu d'une page Web dans Xview Webview?

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

Vous devez accepter la bonne réponse. – elp

+0

@elpsk upvoted tous ceux utiles, posté ma propre réponse en fonction de chacun d'eux –

+3

Est-ce que les personnes qui ont répondu à la question ont un mauvais service pour upvote afin de répondre à votre propre question avec leurs réponses? Juste dire que ce n'est probablement pas la meilleure pratique. –

Répondre

5

OPTION 1

Utilisez cette

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

Lire le contenu du fichier dans votre projet dans un NSString. Ensuite, utilisez la méthode ci-dessus pour charger le contenu html

Utilisez

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

pour obtenir la chaîne à partir du fichier puis utilisez

[webView loadHTMLString:urHTMLString baseURL:baseURL]; 

OPTION 2

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

MISE À JOUR

- (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 enfant hey les gars! merci beaucoup mais aucune de ces méthodes n'a fonctionné pour moi:/ pourriez-vous peut-être prendre le code que j'ai écrit là-bas et le modifier pour me montrer comment c'est fait? –

+1

Ajoutez votre fichier html au regroupement de ressources de projets et utilisez ce code. – 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]; 

des thats wat j'avais utilisé

2

html web local

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

http web

[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; 
} 
Questions connexes