Thursday, 27 February 2014

code for Save data into plist using objective C and retrieve from and display with uitableview..


/*
 1) Create a list of paths.
 2) Get a path to your documents directory from the list.
 3) Create a full file path.
 4) Check if file exists.
 5) Get a path to your plist created before in bundle directory (by Xcode).
 6) Copy this plist to your documents directory.
 */



- (IBAction)SaveUsingPlist:(id)sender 
{
    NSArray *paths = NSSearchPathForDirectoriesInDomains (NSDocumentDirectory, NSUserDomainMask, YES);
    NSString *documentsPath = [paths objectAtIndex:0];
    NSString *plistPath = [documentsPath stringByAppendingPathComponent:@"plist.plist"];
    NSMutableArray *friend = [NSMutableArray arrayWithContentsOfFile:plistPath];
    
    mutableArry = friend;

    
     if([[txtName.text checkNullFromString:txtName.text] isEqualToString:@""]|| [[txtCity.text checkNullFromString:txtCity.text] isEqualToString:@""]|| [[txtPass.text checkNullFromString:txtPass.text] isEqualToString:@""])
    {
        UIAlertView* alert = [[UIAlertView alloc]initWithTitle:@"Required" message:@"Empty Field not allowed !!" delegate:self cancelButtonTitle:@"OK" otherButtonTitles:nil, nil];
        [alert show];
        
        if([mutableArry count]>0)
        {
            [self.tblEmpDetails reloadData];
        }
        return;
    }
    
    if (nil == friend)
    {
        friend = [[NSMutableArray alloc] initWithCapacity:0];
        
        NSMutableDictionary *array = [[NSMutableDictionary alloc]init];
        [array setObject:txtName.text forKey:@"name"];
        [array setObject:txtCity.text forKey:@"city"];
        [array setObject:txtPass.text forKey:@"pass"];
        [friend addObject:array];
        [friend writeToFile:plistPath atomically: TRUE];
        
        UIAlertView* alert = [[UIAlertView alloc]initWithTitle:@"Success" message:@"Saved successfully !!" delegate:self cancelButtonTitle:@"OK" otherButtonTitles:nil, nil];
        [alert show];
        
    }
    else 
    {
       int j = [friend count];
        for (int i = 0; j>i; i++) 
        {
             NSString* strName = [[friend objectAtIndex:i]valueForKey:@"name"];
            if([strName isEqualToString:txtName.text])
            {
                
                UIAlertView* alert = [[UIAlertView alloc]initWithTitle:@"Exist" message:@"Record allready exist !!" delegate:self cancelButtonTitle:@"OK" otherButtonTitles:nil, nil];
                [alert show];
                return;
            }
        }
        NSMutableDictionary *array = [[NSMutableDictionary alloc]init];
        [array setObject:txtName.text forKey:@"name"];
        [array setObject:txtCity.text forKey:@"city"];
        [array setObject:txtPass.text forKey:@"pass"];
        [friend addObject:array];
        [friend writeToFile:plistPath atomically: TRUE];
        
        
        UIAlertView* alert = [[UIAlertView alloc]initWithTitle:@"Success" message:@"Saved successfully !!" delegate:self cancelButtonTitle:@"OK" otherButtonTitles:nil, nil];
        [alert show];
    }
    
    friend = [NSMutableArray arrayWithContentsOfFile:plistPath];
    mutableArry = friend;
    if([mutableArry count]>0)
    {
        [self.tblEmpDetails reloadData];
    }
    
}

No comments:

Post a Comment