0

At first i just send data to server using GET method, I receive a response like below

2015-11-16 14:21:42.168 smartschool[1963:348015] Item actcode: ZQRTNN68
2015-11-16 14:21:42.169 smartschool[1963:348015] Item parentid: 8

How can i display the activation code to a label of the next viewController.

Here is my code:

#import "RegistrationViewController.h"


@interface RegistrationViewController ()

@end

@implementation RegistrationViewController
{

    NSMutableData *mutableData;

#define URL @"http://192.168.1.166/bustracking/activation/requestActivationCode"
}

- (void)viewDidLoad
{
    [super viewDidLoad];
}

-(IBAction)sendDataUsingGet:(id)sender{

    [self sendDataToServer : @"GET"];


}

-(void) sendDataToServer : (NSString *) method
{

    NSString *parentName  = parent_name.text;
    NSString *contactNumber = contact_number.text;
    NSString *beaconid = @"145801000095";
    //NSString *beaconMacAdd = @"14:58:01:00:00:95";

    if(parentName.length > 0 && contactNumber.length > 0){

        NSLog (@"Getting response from server...");

        NSMutableURLRequest *request = nil;

        // Only Difference between POST and GET is only in the way they send parameters

        if([method isEqualToString:@"GET"]){

            NSString *getURL = [NSString stringWithFormat:@"%@?parent_name=%@&contact_number=%@&beacon_id=%@", URL, parentName, contactNumber, beaconid];
            //url = [NSURL URLWithString: getURL];
            getURL = [getURL stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding];
            NSURL *url =  [NSURL URLWithString: getURL];
            request = [NSMutableURLRequest requestWithURL:url];

            NSLog(@"urlinfo: %@", url);
            NSLog(@"link: %@", getURL);
        }

        [request setHTTPMethod:method];
        [request addValue: @"application/x-www-form-urlencoded; charset=utf-8" forHTTPHeaderField:@"Content-Type"];

        NSURLConnection *connection = [[NSURLConnection alloc] initWithRequest:request delegate:self];

        NSLog(@"connection: %@", connection);

        if( connection )
        {
            mutableData = [NSMutableData new];
        }
        else
        {
            NSLog (@"NO_CONNECTION");
            return;
        }
    }
    else
    {
        NSLog(@"NO_VALUES");
    }

}

#pragma mark NSURLConnection delegates

-(void) connection:(NSURLConnection *) connection didReceiveResponse:(NSURLResponse *)response
{
    [mutableData setLength:0];
}

-(void) connection:(NSURLConnection *)connection didReceiveData:(NSData *)data
{
    [mutableData appendData:data];
}

-(void) connection:(NSURLConnection *)connection didFailWithError:(NSError *)error
{
    NSLog (@"NO_CONNECTION");
    return;
}

-(void)connectionDidFinishLoading:(NSURLConnection *)connection
{
    NSError *error = nil;
    NSDictionary *json = [NSJSONSerialization JSONObjectWithData:mutableData options:kNilOptions error:&error];
    NSArray *fetchedArr = [json objectForKey:@"response"];
    NSString *responseActCode;

    for (NSDictionary *user in fetchedArr)
    {
        responseActCode = [user objectForKey:@"activation_code"];
        NSLog(@"Item actcode: %@", responseActCode);
        NSLog(@"Item parentid: %@", [user objectForKey:@"parent_id"]);
        //NSLog(@"Item actcode: %@", [user objectForKey:@"activation_code"]);
    }

    NSUserDefaults *defaults = [NSUserDefaults standardUserDefaults];
    [defaults setObject:responseActCode forKey:@"HighScore"];
    [defaults synchronize];
    NSLog(@"from data: %@", [defaults objectForKey:@"HighScore"]);


    UIAlertView *alert = [[UIAlertView alloc]
                          initWithTitle:@"Activation Code"
                          message:(@"%@", responseActCode)
                          delegate:nil //or self
                          cancelButtonTitle:@"OK"
                          otherButtonTitles:nil];

    [alert show];
}

@end
Sushil Sharma
  • 2,321
  • 3
  • 29
  • 49

2 Answers2

1

There are many ways to pass data between view controllers. Here's one example: Create a public property in "SomeViewController" class(eg: responseActCodeString) and set that property to your activation code(eg: responseActCode).

SomeViewController *someViewController = [[SomeViewController alloc] initWithNib:@"SomeViewController " bundle:nil];
someViewController.responseActCodeString = responseActCode;
[self pushViewController:someViewController animated:YES];

You can also use notifications, user defaults as an alternatives. Also look at this link.

For storyboards add following method in initial view controller:

-(void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender{
    if([segue.identifier isEqualToString:@"someIdentifier"]){
        SomeViewController*someViewController= (SomeViewController*)segue.destinationViewController;
        someViewController.responseActCodeString = responseActCode;
    }
}
Community
  • 1
  • 1
RoHaN
  • 372
  • 3
  • 14
  • Sir, it is not working, i use story board from my project –  Nov 16 '15 at 07:57
  • Sir, is there a way that i can send back the response of the server? –  Nov 16 '15 at 08:26
  • Yes, create an array object in new view controller and set it to your server response: NSArray *fetchedArr = [json objectForKey:@"response"]; someViewController.responseArray = fetchedArr; – RoHaN Nov 16 '15 at 08:31
  • but how sir? Sorry i am new in this field. the code that you see is sending "name and phone number" to server , and the response is the "activation_code and parent_id".. it display in uialert view.. what i want is to see , when i clicked the ok button in uialertview,, it is automatically send back the response to server –  Nov 16 '15 at 08:35
  • You want to send back the data that you fetched back to the server itself ?? – humblePilgrim Nov 16 '15 at 08:55
  • Why do you want to send response back to server? You'll need to invoke another API request for that. – RoHaN Nov 16 '15 at 08:58
  • Thank you for the answer sir, maybe i can create an array for the next view controller, but how can i display that response in the tableviewcell of the next view controller –  Nov 17 '15 at 03:54
1

If the returning data is in dictionary format, declare nsdictionary in header.h class on which you wan to transfer data, similarly you can use array and string, now if you are using a storyboard, below is the method.

- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender
{
    // Make sure your segue name in storyboard is the same as this line

    if ([[segue identifier] isEqualToString:@"YOUR_SEGUE_NAME_HERE"])
    {
        // Get reference to the destination view controller

        YourViewController *vc = [segue destinationViewController];

        // Pass any objects to the view controller here, like...
        vc.dictionary  = self.dictionary.

    }
}

and you want to transfer data upon cell tap this is what you have to do.

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    [self performSegueWithIdentifier:@"YOUR_SEGUE_NAME_HERE"" sender:self];

}

When "performSegueWithIdentifier" is called, this method automatically redirected to "prepareForSegue" method:

- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender
{


    NSIndexPath *indexPath= [self.tableView indexPathForSelectedRow];


    // Make sure your segue name in storyboard is the same as this line

    if ([[segue identifier] isEqualToString:@"YOUR_SEGUE_NAME_HERE"])
    {
        // Get reference to the destination view controller

        YourViewController *vc = [segue destinationViewController];

        // Pass any objects to the view controller here, like...
now this time its array.
        vc.array= [array[indexpath.row] valueforkey@"jsonkey"];

    }
}
ttarik
  • 3,824
  • 1
  • 32
  • 51
Muhammad Anum
  • 136
  • 16