Reputation: 17253
I have something like this in my code
if(![[NSFileManager defaultManager] createDirectoryAtPath:uploadDirPath withIntermediateDirectories:true attributes:nil error:nil]) {
HTTPLogError(@"Could not create directory at path: %@", uploadDirPath);
}
The contents of uploadDirPath
is this :
/Users/UserJ/Library/Developer/CoreSimulator/Devices/535C67AB-F932-417B-8CC3-9A813F4F402C/data/Containers/Data/Application/347FA1B2-F790-46A6-B0D6-B638CF2F43F0/Music/
The above code works fine in the simulator but when I run it in my phone it gets into the condition saying it cant create the folder.
The content of the string when it shows up on my phone is
/var/mobile/Containers/Data/Application/3E115818-0619-46B0-BD13-BD35EFDF50E3/Music/
Any suggestions on why it cant create the folder on my phone ?
Update:
This is the code that comes before
NSArray* dirPaths = NSSearchPathForDirectoriesInDomains(NSMusicDirectory, NSUserDomainMask, YES);
NSString* MusicDir = [dirPaths objectAtIndex:0];
//------
MultipartMessageHeaderField* disposition = [header.fields objectForKey:@"Content-Disposition"];
NSString* filename = [[disposition.params objectForKey:@"filename"] lastPathComponent];
//--------
if ( (nil == filename) || [filename isEqualToString: @""] ) {
// it's either not a file part, or
// an empty form sent. we won't handle it.
return;
}
//NSString* uploadDirPath = [[config documentRoot] stringByAppendingPathComponent:@"upload"];
NSString* uploadDirPath = [MusicDir stringByAppendingString:@"/"];
BOOL isDir = YES;
if (![[NSFileManager defaultManager]fileExistsAtPath:uploadDirPath isDirectory:&isDir ]) {
[[NSFileManager defaultManager]createDirectoryAtPath:uploadDirPath withIntermediateDirectories:YES attributes:nil error:nil];
}
NSString* filePath = [uploadDirPath stringByAppendingPathComponent: filename];
if( [[NSFileManager defaultManager] fileExistsAtPath:filePath] )
{
//Delete this file if it exists
[[NSFileManager defaultManager] removeItemAtPath:filePath error:nil];
}
I noticed that the array when run on Iphone is empty but when it runs on simulator its fine
NSArray* dirPaths = NSSearchPathForDirectoriesInDomains(NSMusicDirectory, NSUserDomainMask, YES);
Upvotes: 0
Views: 251
Reputation: 38162
I believe since application in iOS device runs in sandbox manner, AppData folder structure is predefined. Please try creating your Music directory inside Documents directory and it should work:
NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
NSString *documentsDirPath = paths.firstObject;
NSString *uploadDirPath = [documentsDirPath stringByAppendingPathComponent:@"Music"];
if (![[NSFileManager defaultManager] createDirectoryAtPath:uploadDirPath withIntermediateDirectories:true attributes:nil error:nil]) {
HTTPLogError(@"Could not create directory at path: %@", uploadDirPath);
}
Upvotes: 2