read write ios issue in application start
I added text file to the mainbundle and I write line on it to control the opening of my application like this
NSString *fileNa开发者_运维百科me = [[NSBundle mainBundle] pathForResource:@"TestCases" ofType:@"txt"];
NSString *content = [[NSString alloc] initWithContentsOfFile:fileName
usedEncoding:nil
error:nil];
if ([content length] == 0 ) {
[self MoveAndBuild ];
}
content = @"Downloaded";
[content writeToFile:fileName
atomically:NO
encoding:NSStringEncodingConversionAllowLossy
error:nil];
what is supossed is that [self MoveAndBuild ]; be called just one time after installing the application , but it be called every time the application be opened , am I missing something
best regards
Files in your application's bundle are read only. I'd use NSUserDefaults in your case. E.g.
NSUserDefaults *prefs = [NSUserDefaults standardUserDefaults];
Then you can read a value with
BOOL downloaded = [prefs boolForKey:@"downloaded"]
And write with
[prefs setBool:YES forKey:@"downloaded"];
[content writeToFile:fileName atomically:NO encoding:NSStringEncodingConversionAllowLossy error:nil];
will never be successful because fileName points to the app bundle, which is non writable. So you are testing against the same file all the time.
If you would have used proper error handling you would have seen that.
NSError *error;
if (![content writeToFile:fileName atomically:NO encoding:NSStringEncodingConversionAllowLossy error:&error]) {
NSLog(@"Error writing - %@", error);
}
btw. NSStringEncodingConversionAllowLossy
is not a valid encoding
精彩评论