Wednesday, July 31, 2013

Submit an array through parse in iOS

Awesome to work with PARSE.....

No need of WebServices .. no need of databases it manages it all ..

1) Go to parse.com register there..
2) You will get Client and application ID

#import <Parse/Parse.h> 

Add that in your appdelegate.m like: -

[Parse setApplicationId:@"........Your ID"
                  clientKey:@"... Your ID..."];
    [PFAnalytics trackAppOpenedWithLaunchOptions:launchOptions];


The Code in ViewController.m File: - this is binded with button ACTION (IBAction):

In .Xib 

I have taken 3 text fields: -

-- > txt1 , txt2 , txt3 put all that in a n array *arr

--> Column names in an array  * fields.

// --------------- REST SEE THE CODE BELOW ----------------------------

-(IBAction)SaveData:(id)sender
{
    NSArray * arr=[[NSArray alloc]initWithObjects:txt1.text,txt2.text,txt3.text, nil];
    NSArray *fields=[[NSArray alloc]initWithObjects:@"Name",@"age",@"Gender", nil];
    PFObject *obj=[PFObject objectWithClassName:@"DemoViewController"];
    int i;
    for(i=0;i<[arr count];i++)
    {
        [obj setObject:[arr objectAtIndex:i] forKey:[fields objectAtIndex:i]];
    }
    [obj saveInBackgroundWithBlock:^(BOOL succeeded, NSError *error) {
        if(error == Nil)
        {
            UIAlertView *alert=[[UIAlertView alloc]initWithTitle:@"Saved" message:@"Data Saved" delegate:self cancelButtonTitle:@"OK" otherButtonTitles:nil, nil];
            [alert show];
        }
   
        else
        {
            UIAlertView *alert=[[UIAlertView alloc]initWithTitle:@"Not Saved" message:@"Data Not Saved" delegate:self cancelButtonTitle:@"OK" otherButtonTitles:nil, nil];
            [alert show];
        }
       
     }];
}