PRADIP KUMAR PRADIP KUMAR - 5 months ago 16
Objective-C Question

Post data after submission can't able to fetch

I am trying to implement the guest login. the data is saving after submitting the details to api and when redirecting to next page to see details the data is not coming from api. please tell me where is the problem. I am attaching few codes so that you can able to understand whats happening?

Code for submitting guest detail

- (IBAction)guestSubmit:(id)sender {
if ([self validateBool]) {
BOOL check=[[AppDelegate appDelegate] checkReachability];
if (check) {
dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^{
NSDictionary * dic=[ServiceHelper Guest:[self guestJsonString]];
dispatch_async(dispatch_get_main_queue(),^{
NSArray *aKey = [dic allKeys];
NSLog(@"Akey" ,aKey);
if ([[aKey objectAtIndex:0] isEqualToString:@"success"]) {

UIAlertView * alert =[[UIAlertView alloc]initWithTitle:@"Success" message:[dic valueForKey:[aKey objectAtIndex:0]] delegate:self cancelButtonTitle:@"Ok" otherButtonTitles:nil, nil];
alert.tag=25;
[alert show];
///
NSString * pCode=[[NSUserDefaults standardUserDefaults]valueForKey:@"postcode"];
if (pCode.length==0) {
[[NSUserDefaults standardUserDefaults]setObject:[dic valueForKey:@"fname"] forKey:@"fname"];
[[NSUserDefaults standardUserDefaults]setObject:[dic valueForKey:@"lname"] forKey:@"lname"];
[[NSUserDefaults standardUserDefaults]setObject:[dic valueForKey:@"phone"] forKey:@"phone"];
[[NSUserDefaults standardUserDefaults]setObject:[dic valueForKey:@"email"] forKey:@"email"];
CollectionViewController * CVC=[[CollectionViewController alloc]initWithNibName:@"CollectionViewController" bundle:nil];
[self.navigationController pushViewController:CVC animated:YES];

} else {
[[NSUserDefaults standardUserDefaults]setObject:[dic valueForKey:@"fname"] forKey:@"fname"];
[[NSUserDefaults standardUserDefaults]setObject:[dic valueForKey:@"lname"] forKey:@"lname"];
[[NSUserDefaults standardUserDefaults]setObject:[dic valueForKey:@"phone"] forKey:@"phone"];
[[NSUserDefaults standardUserDefaults]setObject:[dic valueForKey:@"email"] forKey:@"email"];
DeliveryPaymentViewController * DPVC=[[DeliveryPaymentViewController alloc]initWithNibName:@"DeliveryPaymentViewController" bundle:nil];
[self.navigationController pushViewController:DPVC animated:YES];
}



} else {

UIAlertView * alert =[[UIAlertView alloc]initWithTitle:@"Error" message:[dic valueForKey:[aKey objectAtIndex:0]] delegate:self cancelButtonTitle:@"Ok" otherButtonTitles:nil, nil];
[alert show];
}
});
});
}
}


enter image description here

The data is saved in Api. see the code below

-(NSString*)guestJsonString
{
NSMutableString *jsonStringBuilder = nil;
NSString *jsonString = @"";

@try
{
if (jsonStringBuilder == nil)
{
jsonStringBuilder = [[NSMutableString alloc] init];
}

[jsonStringBuilder appendFormat:@"{"];
[jsonStringBuilder appendFormat:@"\"app_id\":\"%@\",",@"2"];
[jsonStringBuilder appendFormat:@"\"request\":\"%@\",",@"guest"];
[jsonStringBuilder appendFormat:@"\"fname\":\"%@\",",guestFname.text];
[jsonStringBuilder appendFormat:@"\"lname\":\"%@\",",guestLname.text];
[jsonStringBuilder appendFormat:@"\"phone\":\"%@\",",guestPhoneNo.text];
[jsonStringBuilder appendFormat:@"\"email\":\"%@\"",guestEmail.text];
[jsonStringBuilder appendFormat:@"}"];
jsonString = jsonStringBuilder;
}
@catch (NSException *exception)
{
jsonString = @"";
}
@finally
{

[[NSUserDefaults standardUserDefaults]setObject:jsonString forKey:@"guestjsonString"];

return jsonString;


}}


After submitting no data is there. see the screenshots

enter image description here

**ServiceHelper.m file **

+(NSDictionary *)Guest:(NSString *)JsonString {
NSDictionary *returnResponse;
NSData *postData = [NSData dataWithBytes:[JsonString UTF8String] length:[JsonString length]];
NSString *serviceOperationUrl = [[NSString stringWithFormat:@"http://iphone.eposapi.co.uk"]stringByAddingPercentEscapesUsingEncoding:NSASCIIStringEncoding];
returnResponse=[self stringWithUrl:[NSURL URLWithString:serviceOperationUrl] postData:postData httpMethod:@"POST"];

return returnResponse;}

Answer

Check the data parsing.

     NSString * pCode=[[NSUserDefaults standardUserDefaults]valueForKey:@"postcode"];
                if (pCode.length==0) {
                    [[NSUserDefaults standardUserDefaults]setObject:[dic valueForKey:@"fname"] forKey:@"fname"];
                    [[NSUserDefaults standardUserDefaults]setObject:[dic valueForKey:@"lname"] forKey:@"lname"];
                    [[NSUserDefaults standardUserDefaults]setObject:[dic valueForKey:@"phone"] forKey:@"phone"];
                    [[NSUserDefaults standardUserDefaults]setObject:[dic valueForKey:@"email"] forKey:@"email"];
                    CollectionViewController * CVC=[[CollectionViewController alloc]initWithNibName:@"CollectionViewController" bundle:nil];
                    [self.navigationController pushViewController:CVC animated:YES];

whether its retrieving the data or not. I think here is the error.

Comments