How to make a path in objective C? - ios

I'm working with Objective C (IOS) and I found this code:
Person *aPerson = <#Get a Person#>;
NSString *archivePath = <Path for the archive#>;
BOOL success = [NSKeyedArchiver archiveRootObject:aPerson toFile:archivePath];
I would like to know what the archivePath should be to save the file in documents directory.
Thank you

To figure out archivePath:
NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory,
NSUserDomainMask, YES);
NSString *documentsPath = [paths objectAtIndex:0]; //Get the docs directory
NSString *archivePath = [documentsPath stringByAppendingPathComponent:#"Person"];

Related

Image not access in iOS 6 from document library?

I have to access image from document library in iOS 6 .
strImgSign = [NSString stringWithFormat:#"%#/Documents//%#", NSHomeDirectory(),strImgSign];
NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
NSString *basePath = ([paths count] > 0) ? [paths objectAtIndex:0] : nil;
NSString *strImgSign = [[arrNameFinal objectAtIndex:indexPath.row] objectForKey:#"signimage"];
/var/mobile/Applications/D912ACC0-5B23-49B2-B9F1-E45ECD713553/Documents//docSign6167Feb13,2015-12.03.26.png
But image access in iOS 7,8 is perfect Working.
You should not build the directory from the NSHoneDirectory or use path separators in you string.
Correct way to build directories:
NSString *imagePath = [NSHomeDirectory() stringByAppendingPathComponent:#"Documents"];
imagePath = [imagePath stringByAppendingPathComponent:strImgSign];
But even better and only correct way is to use the correct directory:
NSString *documentPath =[NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES) firstObject];
NSString *imagePath = [documentPath stringByAppendingPathComponent:strImgSign];
There is an extra / in your path:
strImgSign = [NSString stringWithFormat:#"%#/Documents//%#", NSHomeDirectory(),strImgSign];
Change that to:
strImgSign = [NSString stringWithFormat:#"%#/Documents/%#", NSHomeDirectory(),strImgSign];
But as mentioned by #rckoenes, it's not the correct way of building file system paths. If your image is located in document directory you can use:
NSString *docDirectory = [NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES) objectAtIndex:0];
NSString *yourImagePath = [docDirectory stringByAppendingPathComponent:strImgSign];
But I'm not sure how it worked for you in iOS 7 and iOS 8
NSString *filename = [filepart lastObject];
NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
NSString documentsDirectory = [paths objectAtIndex:0];
NSString inboxPath = [documentsDirectory stringByAppendingPathComponent:#"Inbox"];
NSString *filePath = [inboxPath stringByAppendingPathComponent:filename];

How to delete files cached in supporting files?

I use the following code to store an NSMutableArray into a file:
NSArray *paths = NSSearchPathForDirectoriesInDomains(NSApplicationSupportDirectory, NSUserDomainMask, YES);
NSString *documentsDirectory = [paths objectAtIndex:0];
NSString *filePathHit = [documentsDirectory stringByAppendingPathComponent:#"hit"];
[NSKeyedArchiver archiveRootObject:self.arrayHit toFile:filePathHit];
and the code below to retrieve data from the file:
NSArray *paths = NSSearchPathForDirectoriesInDomains(NSApplicationSupportDirectory, NSUserDomainMask, YES);
NSString *documentsDirectory = [paths objectAtIndex:0];
NSString *filePathHit = [documentsDirectory stringByAppendingPathComponent:#"hit"];
NSMutableArray *unarchivedHit = [NSKeyedUnarchiver unarchiveObjectWithFile:filePathHit];
It works fine but the file been written to seems reside there all the time, so each time I relaunch my application, it reads data from there. But what I want to achieve is the data only resides there during the application operates, and once the app's shut down the data been cleared. How can I achieve this? Thanks.
You can use NSFileManager:
NSError *error = nil;
[[NSFileManager defaultManager] removeItemAtPath:filePathHit error:&error];

How to copy text file from document directory in iOS

Hello i am trying to copy text file from document directory into folder name Temp that located in document directory.
Here is some of code i tried and didn't work.
self.fileManager = [NSFileManager defaultManager];
NSError *error;
NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
NSString *documentsDirectory = [paths objectAtIndex:0];
NSString *documentsDirectoryToTemp = [NSString stringWithFormat:#"%#/Temp/",[paths objectAtIndex:0]];
NSString *txtPath = [documentsDirectory stringByAppendingPathComponent:[NSString stringWithFormat:#"%#.txt",tblGetNameToMove.textLabel.text]];
[self.fileManager copyItemAtPath:txtPath toPath:documentsDirectoryToTemp error:&error];
How can i do that?
There could be many reasons for this :
First try to print all the path like documentsDirectoryToTempn and txtPath whether it is correct or not .
The second thing is make use that error variable :
if(error)
{
NSLog("Error while coping :%#",[error localizedDiscription]);
}
so you can track exact problem .
Here is solution. I need to add directory and also name of file and extension.
Here is codes.
self.fileManager = [NSFileManager defaultManager];
NSError *error;
NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
NSString *documentsDirectory = [paths objectAtIndex:0];
NSString *txtPath = [documentsDirectory stringByAppendingPathComponent:[NSString stringWithFormat:#"%#.txt",tblGetNameToMove.textLabel.text]];
NSString *fileName = [NSString stringWithFormat:#"%#/Temp/%#.txt",documentsDirectory,tblGetNameToMove.textLabel.text];
[self.fileManager copyItemAtPath:txtPath toPath:fileName error:&error];

ipad - write NSString to text file

I have text file text.txt in my xcode project and I need to write NSString init from my iOS app. I've tried this code
NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
NSString *docDir = [paths objectAtIndex: 0];
NSString *docFile = [docDir stringByAppendingPathComponent: #"text.txt"];
NSString *content = #"Test";
[content writeToFile: docFile atomically: NO];
But it doesn't work for me... So can you help me with it?
this worked for me
//Method writes a string to a text file
-(void) writeToTextFile{
//get the documents directory:
NSArray *paths = NSSearchPathForDirectoriesInDomains
(NSDocumentDirectory, NSUserDomainMask, YES);
NSString *documentsDirectory = [paths objectAtIndex:0];
//make a file name to write the data to using the documents directory:
NSString *fileName = [NSString stringWithFormat:#"%#/text.txt",
documentsDirectory];
//create content - four lines of text
NSString *content = #"Test";
//save content to the documents directory
[content writeToFile:fileName
atomically:NO
encoding:NSStringEncodingConversionAllowLossy
error:nil];
}

How to Read a file from documents directory in iOS

I need to search a file in documents directory of iOS but i don't know the extension of that file. How to search the file without extension.
Below is the code if you know the filename.extension(abc.txt). How can i search for the same file with jsut filename(abc)
NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory,
NSUserDomainMask, YES);
NSString *documentsDirectory = [paths objectAtIndex:0];
NSString *filePath = [documentsDirectory stringByAppendingPathComponent:#"abc.txt"];
NSString *content = [NSString stringWithContentsOfFile:filePath
encoding:NSUTF8StringEncoding error:NULL];
NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory,
NSUserDomainMask, YES);
NSString *documentsDirectory = [paths objectAtIndex:0];
NSArray *directoryContent = [[NSFileManager defaultManager] contentsOfDirectoryAtPath:documentsDirectory error:nil];
NSString *fileWithExt = nil;
for (NSString *file in directoryContent )
{
if([file stringByDeletingPathExtension] isEqualsTo:#"abc"]
{
fileWithExt = file;
break;
}
}
P.S. : The fileWithExt will contain the first file with name abc

Resources