Using URLConnection I am downloading a file from a url and storing that data to file in documents directory. When I receive response, when the didReceiveResponse: method called, I create file and move to end of that file this way:
NSString *filePath = [[NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES) objectAtIndex:0] stringByAppendingPathComponent:self.fileName];
NSLog(@"filepath is %@",filePath);
[[NSFileManager defaultManager] createFileAtPath:filePath contents:nil attributes:nil];
file = [[NSFileHandle fileHandleForUpdatingAtPath:filePath] retain];
[file seekToEndOfFile];
where File is object of NSFileHandle class. Later while receiving data, whenever the didReceiveData:(NSData *)data method called, I store the received data to the file this way:
[file seekToEndOfFile];
[file writeData:data];
Finally I close the file in connectionDidFinishLoading: method this way:
[file closeFile];
It was working when I build and run in my system. But when I copied to another system and tried to build I get this error :
Terminating app due to uncaught exception `'NSFileHandleOperationException'`, reason: '*** `-[NSConcreteFileHandle seekToEndOfFile]: No such file or directory'`
I refered http://www.techotopia.com/index.php/Working_with_Directories_on_iPhone_OS and What is NSSearchPathForDirectoriesInDomains? for knowing about NSSearchPathForDirectoriesInDomain function but I am still not clear.
Will UserDomainMask
return the proper path depending on system and user in which the project exists. If so why I am getting such error?
They say the path returned (stored in filepath) will be the path /Users//Library/Application Support/iPhone Simulator/User/Application//Documents where
is the name of the user currently logged into the Mac OS X system on which the simulator is running and is the unique ID of the app
Can anyone help me, to understand why I am getting such error.
As On your Requirment No Source code and you showing the Exception so we have these path for wrting and reading. and share what you using ?
Documents
NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
NSString *documentsDirectory = [paths objectAtIndex:0];
Library
NSArray *paths = NSSearchPathForDirectoriesInDomains(NSLibraryDirectory, NSUserDomainMask, YES);
NSString *libraryDirectory = [paths objectAtIndex:0];
cache directory
NSArray *paths = NSSearchPathForDirectoriesInDomains(NSCachesDirectory, NSUserDomainMask, YES);
NSString *cachePath = [paths objectAtIndex:0];
BOOL isDir = NO;
NSError *error;
if (! [[NSFileManager defaultManager] fileExistsAtPath:cachePath isDirectory:&isDir] && isDir == NO) {
[[NSFileManager defaultManager] createDirectoryAtPath:cachePath withIntermediateDirectories:NO attributes:nil error:&error];
}
For reading and wrting you can use the path.And for more see this Answer