1

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];
  • 1
    NSURLConnection was deprecated in iOS 9. It should still work, but you should not be using it for new development. Use NSURLSession instead. – Duncan C Jan 13 '17 at 23:46
  • Can you be less vague? "it works ... not" can be anything. Have you stepped through it in the debugger? Have you looked at the error variable? Are any variables NIL that you don't expect to be? – uliwitness Jan 14 '17 at 06:02
  • http://stackoverflow.com/questions/15749486/sending-an-http-post-request-on-ios/41647469#41647469 This link will help – Dilip Tiwari Jan 14 '17 at 06:52

1 Answers1

1

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

Community
  • 1
  • 1
user3182143
  • 9,459
  • 3
  • 32
  • 39
  • Now I've got this "App Transport Security has blocked a cleartext HTTP (http://) resource load since it is insecure. Temporary exceptions can be configured via your app's Info.plist file." – Rodrigo Chavarria Jan 16 '17 at 15:11
  • http://stackoverflow.com/questions/31254725/transport-security-has-blocked-a-cleartext-http – user3182143 Jan 19 '17 at 14:04