Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/objective-c/23.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
如何在iPhone中使用HTML文件_Iphone_Objective C_Ios - Fatal编程技术网

如何在iPhone中使用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

我想在我的代码中使用HTML文件,目前我正在使用以下代码行:

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