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

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/5935955/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-30 19:50:53  来源:igfitidea点击:

How can I load a local HTML file instead of a web page in Xcode webview?

iphoneobjective-cxcodeios

提问by Peter V

Hey! How can I load a local html file saved to the project instead of a webpage in this code:

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

采纳答案by visakh7

OPTION 1

选项1

Use this

用这个

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

Read the contents of the file in your project into an NSString. Then use the above method to load the html content

将项目中文件的内容读入NSString. 然后使用上面的方法来加载HTML内容

Use

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

to obtain the string from the file and then use

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

[webView loadHTMLString:urHTMLString baseURL:baseURL];

OPTION 2

选项 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];
}

回答by Virat Naithani

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];

thats wat i had used

那就是我用过的

回答by MrTurki

local web html

本地网页 html

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

http web

http 网络

[webView loadRequest:[NSURLRequest requestWithURL:[NSURL URLWithString:@"http://google.com"]]];

回答by Charlie

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];

回答by user2273042

 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;
}