NSFileManager works fine on simulator but not on device
- by Jenicek
Hi, I have problem creating directories and files with NSFileManager on the iPhone device.
My code, shown below, works fine on the simulator, but not on the device, could you please help me? Gimme some directions where the problem may be, thanks for every reply..
I'm first creating directories this way:
NSFileManager *fileMgr = [NSFileManager defaultManager];
NSArray *arPaths = NSSearchPathForDirectoriesInDomains(NSDownloadsDirectory, NSUserDomainMask, YES);
NSString *appPath = [[NSString alloc] init];
appPath = [arPaths objectAtIndex:0];
strDownloadsDir = [[NSString alloc] init];
strDownloadsDir = [[appPath stringByAppendingPathComponent:@"/Other"] copy];
if(![fileMgr fileExistsAtPath:strDownloadsDir])
[fileMgr createDirectoryAtPath:strDownloadsDir attributes:nil];
and then I'm trying to create new file in this directory this way:
NSString *filePath = [strDownloadsDir stringByAppendingPathComponent:strDlFileName];
//Test whether this file exists, if not, create it
NSLog(@"%@", filePath);
if(![fileMgr fileExistsAtPath:filePath])
{
if([fileMgr createFileAtPath:filePath contents:nil attributes:nil])
NSLog(@"Creating new file at path %@", filePath);
else
NSLog(@"Failed to create new file.");
}
It seems that there's something wrong with whole NSFileManager, because when I'm using fileExistAtPath with a path given by this
NSArray *arPaths = NSSearchPathForDirectoriesInDomains(NSDownloadsDirectory, NSUserDomainMask, YES);
NSString *appPath = [[NSString alloc] init];
appPath = [arPaths objectAtIndex:0];
it is not working too, I tried to change directory to NSDocumentsDirectory but it did not help