A.sonu A.sonu - 5 months ago 23
iOS Question

Response in alert view

empty value in alertviewI am new to IOS i want to display response from post in alert view.in nslog i showed response. i need when i clicked button alert view can display my response.

coding:

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


NSData *postData = [str dataUsingEncoding:NSASCIIStringEncoding allowLossyConversion:YES];
NSString *postLength = [NSString stringWithFormat:@"%lu", (unsigned long)[str length]];

NSMutableURLRequest *request = [[NSMutableURLRequest alloc] initWithURL:[NSURL URLWithString:URL]];
NSLog(@"%@",str);

[request setHTTPMethod:@"POST"];
[request setValue:postLength forHTTPHeaderField:@"Content-Length"];
[request setHTTPBody:postData];

NSURLConnection *theConnection = [NSURLConnection connectionWithRequest:request delegate:self];

if( theConnection ){

mutableData = [[NSMutableData alloc]init];
}
}


alerview:

- (IBAction)butt1:(id)sender {

UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"Value"
message:@"%@"
delegate:self
cancelButtonTitle:@"Cancel"
otherButtonTitles:@"ok", nil];

[self sendDataToServer :@"POST" params:str];

[alert show];

}


post method delegates:
here i get response in json111 that i showed in nslog successfully but in alert view i failed

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

-(void) connection:(NSURLConnection *)connection didFailWithError:(NSError *)error
{
return;
}

-(void)connectionDidFinishLoading:(NSURLConnection *)connection
{


NSError* error111;
json111 = [NSJSONSerialization JSONObjectWithData: mutableData
options:kNilOptions
error:&error111];
NSLog(@"%@",json111);



}

[![emptyvalue in alertview][1]][1]

Answer

change this into

updated answer

@interface myViewController : UIViewController <UIAlertViewDelegate> 


-(void)connectionDidFinishLoading:(NSURLConnection *)connection
{


NSError* error111;
json111 = [NSJSONSerialization JSONObjectWithData: mutableData
                                       options:kNilOptions
                                         error:&error111];
NSLog(@"%@",json111);

NSArray *temp = [ json111 objectForKey:@"Currencies"];

 // you can directly fetch like
 NSDictionary *fetchDict = [temp objectAtIndex:0];
  NSDictionary *fetchUnit = [temp objectAtIndex:1];
 // finally you can fetch like
  NSString * total = [fetchDict objectForKey:@"total"];
   NSString * one_unit = [fetchUnit objectForKey:@"one_unit"];

  //updated
  NSString *final = [NSString stringWithFormat:@"%@ , %@", total,one_unit];

 // in here you can assign the value to Alert


UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"Value"
                                                message:final
                                               delegate:self
                                      cancelButtonTitle:@"Cancel"
                                      otherButtonTitles:@"ok", nil];

 alert.tag = 100;
[alert show];


}




- (void)alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex
{ 
  if (alertView.tag == 100)
{
if (buttonIndex == 0)
 { 
  // ok button pressed
  //[self sendDataToServer :@"POST" params:str];
}else
 {
   // cancel button pressed
}
}