11
NSURL *url = [NSURL URLWithString:[NSString stringWithFormat:@"http:///];
NSURLRequest *req = [[NSURLRequest alloc]initWithURL:url];
NSURLConnection *con = [[NSURLConnection alloc]initWithRequest:req delegate:self startImmediately:YES];

In my project I used sendSynchronousRequest on NSURLConnection. It gives me crash sometimes.

So I convert this code to AsynchronousRequest. I could not find suitable code.

Somebody give me link or post code which suitable to my code. Any hep will be appreciated.

rmaddy
  • 314,917
  • 42
  • 532
  • 579
Xcode
  • 455
  • 2
  • 5
  • 16
  • 2
    You're likely to have this question closed as being too open-ended and localized. I would suggest that you describe what you're trying to do and what you've tried and why it isn't working so that people can answer the request directly. There are lots of folks willing and able to help, but the more specific questions that indicate what you've tried and why it doesn't work tend to be the ones that get the best answers. – gaige May 17 '13 at 11:32
  • @gaige I love how its not been closed :) – Supertecnoboff Jun 06 '15 at 10:57

2 Answers2

21

There are couple of things you could do.

  1. You can use sendAsynchronousRequest and handle the callback block.
  2. You can use AFNetworking library, which handles all your requests in asynchronous fashion. Very easy to use and set up.

Code for option 1:

NSURL *url = [NSURL URLWithString:urlString];
NSURLRequest *urlRequest = [NSURLRequest requestWithURL:url];
NSOperationQueue *queue = [[NSOperationQueue alloc] init];
[NSURLConnection sendAsynchronousRequest:urlRequest queue:queue completionHandler:^(NSURLResponse *response, NSData *data, NSError *error) {
  if (error) {
    //NSLog(@"Error,%@", [error localizedDescription]);
  }
  else {
    //NSLog(@"%@", [[NSString alloc] initWithData:data encoding:NSASCIIStringEncoding]);
  }
}];

Code for option 2:

You might want to download the library & include it in your project first. Then do the following. You can follow the post on setting up here

NSURL *url = [NSURL URLWithString:@"http://httpbin.org/ip"];
NSURLRequest *request = [NSURLRequest requestWithURL:url];

AFJSONRequestOperation *operation = [AFJSONRequestOperation JSONRequestOperationWithRequest:request success:^(NSURLRequest *request, NSHTTPURLResponse *response, id JSON) {
    NSLog(@"IP Address: %@", [JSON valueForKeyPath:@"origin"]);
} failure:nil];

[operation start];
SimpleJ
  • 13,812
  • 13
  • 53
  • 93
Harish
  • 1,469
  • 16
  • 43
7

As an alternative to NSURLConnection's now deprecated sendAsynchronousRequest:queue:completionHandler: method, you could instead use NSURLSession's dataTaskWithRequest:completionHandler: method:

NSURLRequest *request = [NSURLRequest requestWithURL:[NSURL URLWithString:@"https://www.example.com"]];

NSURLSession *session = [NSURLSession sharedSession];
NSURLSessionDataTask *task = [session dataTaskWithRequest:request completionHandler:^(NSData * _Nullable data, NSURLResponse * _Nullable response, NSError * _Nullable error) {
    if (!error) {
        // Option 1 (from answer above):
        NSString *string = [[NSString alloc] initWithData:data encoding:NSASCIIStringEncoding];
        NSLog(@"%@", string);

        // Option 2 (if getting JSON data)
        NSError *jsonError = nil;
        NSDictionary *dictionary = [NSJSONSerialization JSONObjectWithData:data options:kNilOptions error:&jsonError];
        NSLog(@"%@", dictionary);
    }
    else {
        NSLog(@"Error: %@", [error localizedDescription]);
    }
}];
[task resume];
bdev
  • 2,060
  • 5
  • 24
  • 32