object-c 读取本地index.html文件

iostream  01:16:06

//1.第一种方式:加载本地文件 index.html.

//2.filePath为本地index的地址

NSString *filePath = [[NSBundle mainBundle] pathForResource:@"index" ofType:@"html"];

NSLog(@"filePath=%@",filePath);

NSURL *baseURL = [[NSBundle mainBundle] bundleURL];

[self.webView loadHTMLString:[NSString stringWithContentsOfFile:filePath encoding:NSUTF8StringEncoding error:nil] baseURL:baseURL];

//这种方式需要在Build Phases->Copy Boundle Resources 添加这个index.htlm文件。

//我用的是这种

2.通过NSBundle获取文件路径的细节问题

NSString * mainBuild = [[NSBundle mainBundle] bundlePath];

NSString *strurl= [NSString stringWithFormat:@"%@/index.html",mainBuild];

NSLog(@"strurl=%@",strurl);

UIWebView *web = [[UIWebView alloc] initWithFrame:[[UIScreen mainScreen] bounds]];

web.delegate = self;

[web loadRequest:[NSURLRequest requestWithURL:[NSURL URLWithString:strurl]]];

[self.view addSubview:web];

//这种方式注意index的文件路径文件夹要是蓝色的那种导入方法

NSString *imagePath = [NSString stringWithFormat:@"%@/changshuImages/1.jpg",[[NSBundle mainBundle] bundlePath]];

通过NSBundle来获取项目中的文件路径,然后创建UIImage对象,但是问题是创建出来的UIImage对象始终为null。

后来发现在往项目中拖拽文件的时候,如果想通过bundlePath来获取路径,那么在选择的时候有区别。

一般情况下我们是这样拖拽

Destination:copy items into destination group's folder (if needed)

//导入文件的时候会是逻辑结构,而不是物理结构,目录结构图标如下,黄色的文件夹结构,和构建的Group是一样的

Folders: create groups for any added folders

Add to targets :projectName;

这样的选择我们永远都无法通过bundlePath来获取文件的路径

Destination:Copy items into destination group's folder (if needed)

//创建引入的是蓝色的文件夹结构,物理路径

Folders:create folder references for any added folders

Add to targets: projectName

这样就可以通过bundlePath来获取路径了

©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容