Rodrigo Chavarria
Rodrigo Chavarria

Reputation: 37

Consume webService in JSON in objective C

Ive got this code to consume it works in iOS8 and iOS10 but not in iOS9

BOOL blnAnswer = NO;
  NSString *strAnswer = @"";
  NSURLRequest *urlRequest = [NSURLRequest requestWithURL:[NSURL URLWithString:@"http://10.16.2.42/api/IniciarSesionMobile/GetConnectTest"]
                                              cachePolicy:NSURLRequestReturnCacheDataElseLoad
                                          timeoutInterval:35];
  // Fetch the JSON response
  NSData *urlData;
  NSURLResponse *response;
  NSError *error;

  // Make synchronous request
  urlData = [NSURLConnection sendSynchronousRequest:urlRequest
                                  returningResponse:&response
                                              error:&error];

  // Construct a String around the Data from the response
  strAnswer = [[NSString alloc] initWithData:urlData encoding:NSUTF8StringEncoding];

Upvotes: 1

Views: 760

Answers (1)

user3182143
user3182143

Reputation: 9589

If you want to GET the data from the server,follow the below steps

NSMutableURLRequest *urlRequest = [[NSMutableURLRequest alloc] initWithURL:[NSURL URLWithString:@"http://10.16.2.42/api/IniciarSesionMobile/GetConnectTest"]];

//If you have parameters,pass it to URL

//create the Method "GET"
[urlRequest setHTTPMethod:@"GET"];

NSURLSession *session = [NSURLSession sharedSession];
NSURLSessionDataTask *dataTask = [session dataTaskWithRequest:urlRequest completionHandler:^(NSData *data, NSURLResponse *response, NSError *error) {
  NSString *strResponse = [[NSString alloc] initWithData:data encoding:NSASCIIStringEncoding];
  NSLog(@"response is: %@", strResponse );
}];
[dataTask resume];

Get and Post

Upvotes: 1

Related Questions