File Management in iOS
File Management using UserName Save Process:
NSString *file = @"/Login.txt";
NSString *savedusername = aText.text; //saved text File Value
NSArray *arrayPaths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
NSString *docDirectory = [arrayPaths objectAtIndex:0];
NSString *filePath = [docDirectory stringByAppendingString:file];
[savedusername writeToFile:filePath atomically:YES encoding:NSUTF8StringEncoding error:nil];
File Management using UserName Retrive Process:
NSString *file = @"/Login.txt";
NSArray *arrayPaths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
NSString *docDirectory = [arrayPaths objectAtIndex:0];
NSString *filePath = [docDirectory stringByAppendingString:file];
NSString *fileContents = [NSString stringWithContentsOfFile:filePath encoding:NSUTF8StringEncoding error:nil];
NSLog(@"%@",filePath);
NSLog(@"%@", fileContents); //show the contents of file
File Management using UserName Update Process:
Step 1 : Retrive process to retrive the file contents
Step 2 : Nsstring to save the file contents
Step 3 : Nsstring to add the new content &Old content
Step 4 : Save process to save the new added strings
Example - File Management using UserName Retrive Process with Validation:
NSString *file = @"/Login.txt";
NSArray *arrayPaths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
NSString *docDirectory = [arrayPaths objectAtIndex:0];
NSString *filePath = [docDirectory stringByAppendingString:file];
NSString *fileContents = [NSString stringWithContentsOfFile:filePath encoding:NSUTF8StringEncoding error:nil];
NSLog(@"%@",filePath);
NSLog(@"%@", fileContents); //show the contents of file
//Validation Process
if(fileContents!=nil)
{
if ([fileContents isEqualToString:@"|data missing|"])
{
UIAlertView *objAlert = [[UIAlertView alloc] initWithTitle:@"Message!" message:@"Unable to connect. Please try again" delegate:self cancelButtonTitle:nil otherButtonTitles:@"Try Again",nil];
[objAlert show];
[objAlert release];
UIBarButtonItem *saveButton = [[UIBarButtonItem alloc]
initWithTitle:NSLocalizedString(@"Login", @"Save - for button to save changes")
style:UIBarButtonItemStylePlain
target:self
action:@selector(logP)];
self.navigationItem.rightBarButtonItem = saveButton;
[saveButton release];
}
else if ([fileContents isEqualToString:@"|InvalidUdid |"])
{
UIBarButtonItem *saveButton = [[UIBarButtonItem alloc]
initWithTitle:NSLocalizedString(@"Login", @"Save - for button to save changes")
style:UIBarButtonItemStylePlain
target:self
action:@selector(logP)];
self.navigationItem.rightBarButtonItem = saveButton;
[saveButton release];
}
else if ([fileContents isEqualToString:@"|Deactivated|"])
{
UIBarButtonItem *saveButton = [[UIBarButtonItem alloc]
initWithTitle:NSLocalizedString(@"Login", @"Save - for button to save changes")
style:UIBarButtonItemStylePlain
target:self
action:@selector(logP)];
self.navigationItem.rightBarButtonItem = saveButton;
[saveButton release];
}
else if ([fileContents isEqualToString:@""])
{
UIBarButtonItem *saveButton = [[UIBarButtonItem alloc]
initWithTitle:NSLocalizedString(@"Login", @"Save - for button to save changes")
style:UIBarButtonItemStylePlain
target:self
action:@selector(logP)];
self.navigationItem.rightBarButtonItem = saveButton;
[saveButton release];
}
else if ([fileContents length]>=20)
{
UIBarButtonItem *saveButton = [[UIBarButtonItem alloc]
initWithTitle:NSLocalizedString(@"Login", @"Save - for button to save changes")
style:UIBarButtonItemStylePlain
target:self
action:@selector(logP)];
self.navigationItem.rightBarButtonItem = saveButton;
[saveButton release];
}
else
{
self.username = fileContents;
NSLog(@"%@",username);
}
}
else
{
UIBarButtonItem *saveButton = [[UIBarButtonItem alloc]
initWithTitle:NSLocalizedString(@"Login", @"Save - for button to save changes")
style:UIBarButtonItemStylePlain
target:self
action:@selector(logP)];
self.navigationItem.rightBarButtonItem = saveButton;
[saveButton release];
}
No comments:
Post a Comment