There is a horrible bug which is only occurring intermittently. I'm trying to save data into a sub-folder in 'Application Support' folder. Before saving I will create that sub-folder if not exists
NSString *cachePath = NSSearchPathForDirectoriesInDomains(NSApplicationSupportDirectory, NSUserDomainMask, YES).firstObject;
NSString *packagePath = [cachePath stringByAppendingPathComponent:#"ResourcePackage"];
NSError *error = nil;
BOOL isDirectory = NO;
if(![[NSFileManager defaultManager] fileExistsAtPath:packagePath isDirectory:&isDirectory] || !isDirectory)
{
[[NSFileManager defaultManager] createDirectoryAtPath:packagePath withIntermediateDirectories:YES attributes:nil error:&error];
}
Everything goes fine but except for only one device on which will get error with code 513
Error Domain=NSCocoaErrorDomain Code=513 "您没有将文件“ResourcePackage”存储到文件夹“Application Support”中的权限。" UserInfo={NSFilePath=/var/mobile/Containers/Data/Application/4FF643C4-D568-417F-9A88-5E9FF2326972/Library/Application Support/ResourcePackage, NSUnderlyingError=0x17425e6f0 {Error Domain=NSPOSIXErrorDomain Code=13 "Permission denied"}}
What have I missed or am I doing wrong while writing to this directory?
Thanks!
Related
I am trying to release cache from files by calling this method :
-(void)removeCacheFromFiles
{
NSFileManager *filemgr;
filemgr = [NSFileManager defaultManager];
NSError *error=nil;
if ([filemgr removeItemAtPath: [NSHomeDirectory() stringByAppendingString:#"/Library/Caches"] error: &error] == YES)
NSLog (#"Remove successful");
else
NSLog (#"Remove failed");
NSLog(#"error %#",error);
[filemgr createDirectoryAtPath: [NSHomeDirectory() stringByAppendingString:#"/Library/Caches"] withIntermediateDirectories:NO attributes:nil error:nil];
}
But I get "Remove failed" every time.
I tried this as well and get the same result:
-(void)removeCacheFromFiles
{
NSFileManager *filemgr;
filemgr = [NSFileManager defaultManager];
NSArray *cachePathList = NSSearchPathForDirectoriesInDomains(NSCachesDirectory, NSUserDomainMask, YES);
NSString *cachePath = [cachePathList objectAtIndex:0];
NSError *error=nil;
if ([filemgr removeItemAtPath:cachePath error: &error] == YES)
NSLog (#"Remove successful");
else
NSLog (#"Remove failed");
NSLog(#"error %#");
[filemgr createDirectoryAtPath: [NSHomeDirectory() stringByAppendingString:#"/Library/Caches"] withIntermediateDirectories:NO attributes:nil error:nil];
}
Also where should this remove caching be down ? Is it a good practise to call it in ViewDidDisappear or in ViewDidAppear so that the cache is cleared when the view controller is called the second time ?
The error in the NSLog says :
error Error Domain=NSCocoaErrorDomain Code=513 "“Caches” couldn’t be removed because you don’t have permission to access it." UserInfo={NSUnderlyingError=0x15fac9a0 {Error Domain=NSPOSIXErrorDomain Code=1 "Operation not permitted"}, NSFilePath=/var/mobile/Containers/Data/Application/1795C59B-185F-4607-8682-004CDF3CDED7/Library/Caches, NSUserStringVariant=(
Remove
)}
I create two file paths with this code:
NSString *fileName = [[self genRandStringLength:15] stringByAppendingString:#".mp4"];
NSString *finalFilePath = [self.videoFiles stringByAppendingString:fileName];
NSString *tmpFile = [NSTemporaryDirectory() stringByAppendingPathComponent:[NSString stringWithFormat:#"temp.mp4"]];
[[NSFileManager defaultManager] removeItemAtPath:tmpFile error:NULL];
Then i have some code to create video file and the i want to save it in the Document Folder:
[[NSFileManager defaultManager] copyItemAtPath:tmpFile toPath:finalFilePath error:&err];
And i get this error:
Error Domain=NSCocoaErrorDomain Code=4 "The file “temp.mp4” doesn’t exist."
UserInfo={NSSourceFilePathErrorKey=/private/var/mobile/Containers/Data/Application/E5B7BBCB-311F-41A3-949D-8A309F535C95/tmp/temp.mp4, NSUserStringVariant=(
Copy
), NSDestinationFilePath=/var/mobile/Containers/Data/Application/E5B7BBCB-311F-41A3-949D-8A309F535C95/Documents/VideoFiles/0iWDmhLvbbzM6SE.mp4, NSFilePath=/private/var/mobile/Containers/Data/Application/E5B7BBCB-311F-41A3-949D-8A309F535C95/tmp/temp.mp4, NSUnderlyingError=0x12f0099b0 {Error Domain=NSPOSIXErrorDomain Code=2 "No such file or directory"}}
Any idea what can be the problem?
Just for some checking i try to copy the video to the iPhone Photo library with :
UISaveVideoAtPathToSavedPhotosAlbum(tmpFile, self, nil, nil);
And it's work fine, so the file is exist.
Use this code
NSString *fileName = [[self genRandStringLength:15] stringByAppendingString:#".mp4"];
NSString *finalFilePath = [self.videoFiles stringByAppendingString:fileName];
NSString *tmpFile = [NSTemporaryDirectory() stringByAppendingPathComponent:[NSString stringWithFormat:#"temp.mp4"]];
NSFileManager *fileManager = [NSFileManager defaultManager];
BOOL success = [fileManager fileExistsAtPath:tmpFile];
if (success)
{
[[NSFileManager defaultManager] copyItemAtPath:tmpFile toPath:finalFilePath error:&err];
[[NSFileManager defaultManager] removeItemAtPath:tmpFile error:NULL];
}
I have this code and error with text:
ERROR: Error Domain=NSCocoaErrorDomain Code=4 "The operation couldn’t be completed. (Cocoa error 4.)" UserInfo=0x145b5a60 {NSSourceFilePathErrorKey=/private/var/mobile/Containers/Data/Application/E905FE5A-C39B-41F0-8BC7-FC58CC3F4306/Library/Caches/2D0404EE-746C-4C1E-98F3-42FFE485BE3B.zip, NSUserStringVariant=(
Move
), NSFilePath=/private/var/mobile/Containers/Data/Application/E905FE5A-C39B-41F0-8BC7-FC58CC3F4306/Library/Caches/2D0404EE-746C-4C1E-98F3-42FFE485BE3B.zip, NSDestinationFilePath=/var/mobile/Containers/Data/Application/E905FE5A-C39B-41F0-8BC7-FC58CC3F4306/Documents/Files/temp.zip, NSUnderlyingError=0x1457af70 "The operation couldn’t be completed. No such file or directory"}
NSFileManager *fileManager = [NSFileManager defaultManager];
NSArray *pathDocumentDirectory = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
NSString *dictionaryName = #"Files";
NSString *filesPath = [[pathDocumentDirectory objectAtIndex:0] stringByAppendingPathComponent:dictionaryName];
NSString *toPath = [filesPath stringByAppendingPathComponent:#"temp.zip"];
NSString *fromPath = downloadedItem.contentURL.path;
NSError *error = nil;
BOOL isDir = YES;
if([fileManager fileExistsAtPath:fromPath])
{
// Create PurchasedBooks directory
if (![fileManager fileExistsAtPath:dictionaryName isDirectory:&isDir]) {
[fileManager createDirectoryAtPath:dictionaryName withIntermediateDirectories:NO attributes:nil error:&error];
}
[[NSFileManager defaultManager]moveItemAtPath:fromPath toPath:toPath error:&error];
if (error) {
NSLog(#"ERROR: %#", error.description);
}
}
Really do not understand why this happens
Add error checking for:
[fileManager createDirectoryAtPath:dictionaryName withIntermediateDirectories:NO attributes:nil error:&error];
I'm trying to copy a json file from the application bundle to the Document directory, but surprisingly i'm not able to do that. Here's the code:
NSArray *array = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
NSString *docDir = [array lastObject];
NSString *path = [docDir stringByAppendingPathExtension:#"themes.json"];
if (![[NSFileManager defaultManager] fileExistsAtPath:path])
{
NSString *themesPath = [[NSBundle mainBundle] pathForResource:#"themes" ofType:#"json"];
NSError *error = nil;
[[NSFileManager defaultManager] copyItemAtPath:themesPath toPath:path error:&error];
if (error)
NSLog(#"Error %#", error);
}
It produces the following error:
Error Domain=NSCocoaErrorDomain Code=513 "The operation couldn’t be completed. (Cocoa error 513.)" UserInfo=0x194a0e90 {NSSourceFilePathErrorKey=/var/mobile/Applications/ACED3EF9-B0D8-49E8-91DE-37128357E509/Frinder.app/themes.json, NSUserStringVariant=(
Copy
), NSFilePath=/var/mobile/Applications/ACED3EF9-B0D8-49E8-91DE-37128357E509/Frinder.app/themes.json, NSDestinationFilePath=/var/mobile/Applications/ACED3EF9-B0D8-49E8-91DE-37128357E509/Documents.themes.json, NSUnderlyingError=0x194a0400 "The operation couldn’t be completed. Operation not permitted"}
After a search i've found this question and tried to modify my code as follows:
if (![[NSFileManager defaultManager] fileExistsAtPath:path])
{
NSString *themesPath = [[NSBundle mainBundle] pathForResource:#"themes" ofType:#"json"];
NSData *data = [NSData dataWithContentsOfFile:themesPath];
BOOL success = [[NSFileManager defaultManager] createFileAtPath:path contents:data attributes:nil];
if (!success)
NSLog(#"Fail");
}
but it doesn't work either. success variable is NO. The last thing i tried was:
[data writeToFile:path atomically:YES];
but still in vain. it returns NO.
I need to note that the issue arises on a device only. On a simulator it works all right.
Can anybody give me a clue?
The first example is correct on one thing:
[docDir stringByAppendingPathExtension:#"themes.json"];
should be:
[docDir stringByAppendingPathComponent:#"themes.json"];
This becomes clear we you read the error message, you see that it tries to write the file to /var/mobile/Applications/ACED3EF9-B0D8-49E8-91DE-37128357E509/Documents.themes.json. Notice that there a . where there should be a /.
The stringByAppendingPathExtension: is used for adding a extension to a file, jpg, txt, html,.....
stringByAppendingPathComponent will add a file to a path, by adding the correct directory separator, you case /.
You second example will definitely fail since the app bundle is readonly.
I'm trying to use the method seen in the solution here to delete all the files in the iPhone documents directory for the application I'm writing. I've made some minor changes to the code in the solution in order to pass in the string location of the documents directory. My version of the code is the following:
NSString *directory = [[[[NSFileManager defaultManager] URLsForDirectory:NSDocumentDirectory inDomains:NSUserDomainMask] lastObject] absoluteString];
NSLog(#"%#", directory);
NSError *error = nil;
NSArray *directoryContents = [[NSFileManager defaultManager] contentsOfDirectoryAtPath:directory error:&error];
if (error == nil) {
for (NSString *path in directoryContents) {
NSString *fullPath = [directory stringByAppendingPathComponent:path];
BOOL removeSuccess = [[NSFileManager defaultManager] removeItemAtPath:fullPath error:&error];
if (!removeSuccess) {
// Error handling
}
}
} else {
// Error handling
NSLog(#"%#", error);
}
When I attempt to run this however, the setting of directoryContents fails due to what's being passed being interpreted as a non-existent directory. Specifically, the two NSLog() statements I have put in the code returns the following:
2013-04-22 11:48:22.628 iphone-ipcamera[389:907] file://localhost/var/mobile/Applications/AB039CDA-412B-435A-90C2-8FBAADFE6B1E/Documents/
2013-04-22 11:48:22.650 iphone-ipcamera[389:907] Error Domain=NSCocoaErrorDomain Code=260 "The operation couldn’t be completed. (Cocoa error 260.)" UserInfo=0x1d5232c0 {NSUnderlyingError=0x1d54c420 "The operation couldn’t be completed. No such file or directory", NSFilePath=file://localhost/var/mobile/Applications/AB039CDA-412B-435A-90C2-8FBAADFE6B1E/Documents/, NSUserStringVariant=(
Folder
)}
As far as I can see the path being printed to the NSLog looks correct so I'm not sure what I'm doing wrong. Can anyone point out to me where my mistake is? Thank you much!
Your code to get the value of directory isn't quite right. You want:
NSURL *directoryURL = [[[NSFileManager defaultManager] URLsForDirectory:NSDocumentDirectory inDomains:NSUserDomainMask] lastObject];
NSString *directory = [directoryURL path];
Calling absoluteString on NSURL gives you a file URL. You don't want a file URL, you want the file URL converted to a file path. This is what the path method does.
Another way is:
NSString *directory = [NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES) lastObject];