-[NSString writeToFile:] does not change the contents of the file
I'm trying to write to a file "index.html" that I have in my resources. I can load the file with no problem, but I can't seem to write to it. Nothing is showing up as an error, it simply doesn't write. The app doesn't abort or anything, but when I re-load the file nothing has changed.
My writing code:
NSBundle *thisBundle = [NSBundle bundleForClass:[self class]];
NSString *path = [thisBundle pa开发者_开发技巧thForResource:@"index" ofType:@"html"];
NSString *myString = [[NSString alloc] initWithFormat:@""];
[myString writeToFile:path atomically:YES encoding:NSUTF8StringEncoding error:NULL];
My loading code:
[myWebView loadRequest:[NSURLRequest requestWithURL:[NSURL fileURLWithPath:[[NSBundle mainBundle] pathForResource:@"index" ofType:@"html"]isDirectory:NO]]];
What am I doing wrong?
The reason that your existing code doesn't overwrite the index.html
file is that an application can not overwrite its resources. Apple's iOS Application Programming Guide specifically says:
This is the bundle directory containing the application itself. Do not write anything to this directory. To prevent tampering, the bundle directory is signed at installation time. Writing to this directory changes the signature and prevents your application from launching again.
Instead, write to your documents directory. You can get the path to the documents directory like this:
NSString * docsDir = [NSHomeDirectory() stringByAppendingPathComponent:@"Documents"];
Note that NSHomeDirectory()
on iOS simple returns the path to your application's bundle. Once you have the path to the documents directory, you can write to a resource, let's say index.html, as follows:
NSString * path = [docsDir stringByAppendingPathComponent:@"index.html"];
[myString writeToFile:path atomically:YES encoding:NSUTF8StringEncoding error:nil];
Note that I changed your error:
parameter to nil
. This will not actually affect anything, but it is common practice to use nil
to indicate NULL Objective-C objects.
Try to move your file to Documents Directory before perform the operation this bunch of code makes the work
.h
#import <Foundation/Foundation.h>
@interface NSFileManager (NSFileManagerAdds)
+ (NSString*) copyResourceFileToDocuments:(NSString*)fileName withExt:(NSString*)fileExt;
@end
.m
#import "NSFileManager + NSFileManagerAdds.h"
@implementation NSFileManager (NSFileManagerAdds)
+ (NSString*) copyResourceFileToDocuments:(NSString*)fileName withExt:(NSString*)fileExt
{
//Look at documents for existing file
NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
NSString *documentsDirectory = [paths objectAtIndex:0];
NSString *path = [documentsDirectory stringByAppendingPathComponent:[NSString stringWithFormat:@"%@.%@", fileName, fileExt]];
NSFileManager* fileManager = [NSFileManager defaultManager];
if(![fileManager fileExistsAtPath:path])
{
NSError *nError;
[fileManager copyItemAtPath:[[NSBundle mainBundle] pathForResource:fileName ofType:fileExt] toPath:path error:&nError];
}
return path;
}
@end
Finally you should use it in something like that:
[NSFileManager copyResourceFileToDocuments:@"index" withExt:@"html"];
精彩评论