如何在iPhone中使用HTML文件
我想在我的代码中使用HTML文件,目前我正在使用以下代码行:如何在iPhone中使用HTML文件,iphone,objective-c,ios,Iphone,Objective C,Ios,我想在我的代码中使用HTML文件,目前我正在使用以下代码行: NSString *htmlFile = [[NSBundle mainBundle] pathForResource:@"index" ofType:@"htm" inDirectory:nil]; NSString* htmlString = [NSString stringWithContentsOfFile:htmlFile encoding:NSUTF8StringEncoding error:nil]; [WebView
NSString *htmlFile = [[NSBundle mainBundle] pathForResource:@"index" ofType:@"htm" inDirectory:nil];
NSString* htmlString = [NSString stringWithContentsOfFile:htmlFile encoding:NSUTF8StringEncoding error:nil];
[WebView loadHTMLString:htmlString baseURL:nil];
但我面临的问题是:HTML页面包含一些图像,所有这些图像都在我的项目中的一个文件夹中,但在运行时我无法在HTML页面中看到这些图像。这里介绍了在项目中使用HTML的可能方法, 下面是将HTML文件加载到xcode项目的代码 示例1,从URNSURL加载内容
NSURL *url = [NSURL URLWithString:@"http://google.com"];
NSURLRequest *request = [NSURLRequest requestWithURL:url];
[webView loadRequest:request];
示例2,从字符串加载内容
NSString *HTMLData = @"<h1>Hello this is a test</h1>";
[webView loadHTMLString:HTMLData baseURL:nil];
希望此代码有助于开发应用程序您可以将baseURL设置为您的资源文件夹,您的图像位于该文件夹中:
NSString *htmlFile = [[NSBundle mainBundle] pathForResource:@"index" ofType:@"htm" inDirectory:nil];
NSString *htmlString = [NSString stringWithContentsOfFile:htmlFile encoding:NSUTF8StringEncoding error:nil];
NSString *resourcePath = [[NSBundle mainBundle]resourcePath];
NSURL *baseUrl = [[NSURL alloc]initFileURLWithPath:resourcePath];
[self.myWebView loadHTMLString:htlmString baseURL:baseUrl];
将图像路径更改为html文件。是的,我这样做了,但不起作用。您需要转换NSData数据
NSString *htmlFile = [[NSBundle mainBundle] pathForResource:@"index" ofType:@"htm" inDirectory:nil];
NSString *htmlString = [NSString stringWithContentsOfFile:htmlFile encoding:NSUTF8StringEncoding error:nil];
NSString *resourcePath = [[NSBundle mainBundle]resourcePath];
NSURL *baseUrl = [[NSURL alloc]initFileURLWithPath:resourcePath];
[self.myWebView loadHTMLString:htlmString baseURL:baseUrl];