Alb*_*haw 2 file-io cocoa-touch objective-c ios
我正在尝试写入我的资源中的"index.html"文件.我可以毫无问题地加载文件,但我似乎无法写入它.没有任何东西显示为错误,它只是不写.该应用程序不会中止或任何东西,但当我重新加载文件时没有任何改变.
我的写作代码:
NSBundle *thisBundle = [NSBundle bundleForClass:[self class]];
NSString *path = [thisBundle pathForResource:@"index" ofType:@"html"];
NSString *myString = [[NSString alloc] initWithFormat:@""];
[myString writeToFile:path atomically:YES encoding:NSUTF8StringEncoding error:NULL];
Run Code Online (Sandbox Code Playgroud)
我的加载代码:
[myWebView loadRequest:[NSURLRequest requestWithURL:[NSURL fileURLWithPath:[[NSBundle mainBundle] pathForResource:@"index" ofType:@"html"]isDirectory:NO]]];
Run Code Online (Sandbox Code Playgroud)
我究竟做错了什么?
您的现有代码不会覆盖该index.html文件的原因是应用程序无法覆盖其资源.Apple的iOS应用程序编程指南专门说:
这是包含应用程序本身的bundle目录.不要在此目录中写任何内容.为防止篡改,bundle目录在安装时签名.写入此目录会更改签名并阻止您的应用程序再次启动.
而是写入您的文档目录.您可以像这样获取文档目录的路径:
NSString * docsDir = [NSHomeDirectory() stringByAppendingPathComponent:@"Documents"];
Run Code Online (Sandbox Code Playgroud)
请注意,NSHomeDirectory()在iOS上,simple返回应用程序包的路径.获得文档目录的路径后,您可以写入资源,比如index.html,如下所示:
NSString * path = [docsDir stringByAppendingPathComponent:@"index.html"];
[myString writeToFile:path atomically:YES encoding:NSUTF8StringEncoding error:nil];
Run Code Online (Sandbox Code Playgroud)
请注意,我将您的error:参数更改为nil.这实际上不会影响任何东西,但通常的做法是nil用来指示NULL Objective-C对象.
| 归档时间: |
|
| 查看次数: |
6200 次 |
| 最近记录: |