Iphone 如何在Xcode webview中加载本地HTML文件而不是网页?

Iphone 如何在Xcode webview中加载本地HTML文件而不是网页?,iphone,objective-c,xcode,ios,Iphone,Objective C,Xcode,Ios,嘿! 如何加载保存到项目中的本地html文件,而不是此代码中的网页: - (void)loadAboutHTML { UIWebView *aboutHTML = [[UIWebView alloc] init]; NSURL *webURL = [NSURL URLWithString:@"http://apple.com"]; NSURLRequest *webURLRequest = [NSURLRequest requestWithURL:webURL]; [aboutHTML load

嘿! 如何加载保存到项目中的本地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];}

选项1

用这个

- (void)loadHTMLString:(NSString *)string baseURL:(NSURL *)baseURL
将项目中文件的内容读入
NSString
。然后使用上述方法加载html内容

使用

从文件中获取字符串,然后使用

[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];
}
这就是我使用的本地网页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"]]];

@问题孩子嘿,伙计们!非常感谢,但这些方法对我都不起作用:/你可以把我在上面写的代码修改一下,让我看看它是如何完成的吗?将你的html文件添加到项目资源包中,然后使用这些代码。你需要接受正确的答案。@elpsk对所有有用的方法都投了赞成票,发布了我自己的答案,基于所有这些,回答问题的人用他们的回答回答你自己的问题,这是一种伤害吗?只是说这可能不是最好的做法。
[webView loadRequest:[NSURLRequest requestWithURL:[NSURL fileURLWithPath:[[NSBundle mainBundle]                                                                           pathForResource:@"file_name_html" ofType:@"html"] isDirectory:NO]]];
[webView loadRequest:[NSURLRequest requestWithURL:[NSURL URLWithString:@"http://google.com"]]];
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];
 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;
}