0

I would like to launch a request with NSMutableURLRequest when the answer of the [NSURLConnection sendAsynchronousRequest: get a good status code. When I launch the executeForStatusOkMetod containing the NSMutableURLRequest alone, it works perfectly.

But if I launch the executeForStatusOkMetod with the [NSURLConnection sendAsynchronousRequest:, the connectionDidFinishLoading: function is never called.

Here is the main code :

 NSOperationQueue *myQueue = [[NSOperationQueue alloc] init];
    NSMutableURLRequest *request = [[NSMutableURLRequest alloc] initWithURL:[NSURL URLWithString:url]];
    request.timeoutInterval = 10;
    [NSURLConnection sendAsynchronousRequest:request queue:myQueue completionHandler:^(NSURLResponse *response, NSData *data, NSError *error) {
        NSHTTPURLResponse *httpResponse = (NSHTTPURLResponse *) response;
        NSLog(@"response status code: %ld, error status : %@", (long)[httpResponse statusCode], error.description);

        if ((long)[httpResponse statusCode] >= 200 && (long)[httpResponse statusCode]< 400)
            {
               // do stuff
                [[DataManagement sharedManager] executeForStatusOkMetod];
            }
    }];

Here is the function called :

(void) executeForStatusOkMetod
{
 NSMutableURLRequest* request = [[NSMutableURLRequest alloc] init];
        NSString *url_string = [bytes stringByAddingPercentEncodingWithAllowedCharacters:[NSCharacterSet URLQueryAllowedCharacterSet]];
        [request setURL:[NSURL URLWithString:[set_send_order stringByAppendingString: url_string]]];
        [request setCachePolicy:NSURLRequestReloadIgnoringLocalCacheData];
        [request setTimeoutInterval:timeOut];
        [NSURLConnection connectionWithRequest:request delegate:self]; 
}

Why connectionDidFinishLoading: is not called with the call of the NSMutableURLRequest located in a [NSURLConnection sendAsynchronousRequest: method ?

ΩlostA
  • 2,501
  • 5
  • 27
  • 63
  • `connectionWithRequest:delegate` returns a `NSURLConnection` object. I guess that you may have to call `start` on it. `NSURLConnection *connection = [NSURLConnection connectionWithRequest:request delegate:self]; [connection start];`? Also, since you use the completion block, it won't call the delegate. It's one or the other. You can force calling the delegate in the block tough. – Larme Feb 27 '17 at 16:00
  • No one should be using `NSURLConnection` anymore, it's very deprecated. `NSURLSession` is what should be used from now on. Or really, AFNetworking or Alamofire. – Jon Shier Feb 27 '17 at 16:13
  • @Larme So if I can't use both, how can I tell him to check first if there is a local connection in Wi-Fi before to call the WS ? – ΩlostA Feb 27 '17 at 19:11
  • You may be interested in Reachability if it's to check your connection. – Larme Feb 28 '17 at 09:08

1 Answers1

0

In the first example the delegate method is not called because the API does not contain a parameter to set the delegate.

In the API with completion handler the connection is finished when the completion handler is executed.

vadian
  • 274,689
  • 30
  • 353
  • 361
  • Do you think I can not use the **NSMutableURLRequest** in the response of the **sendAsynchronousRequest** ? – ΩlostA Feb 27 '17 at 19:14
  • You can, the request is captured into the block. – vadian Feb 27 '17 at 19:16
  • mmm ok and how can I set the delegate in the API ? – ΩlostA Feb 27 '17 at 19:48
  • You can't. Either use `sendAsynchronousRequest` with completion handler or `connectionWithRequest` with delegate. Actually you need the delegate methods only for a finer control for example to handle credentials and redirections. – vadian Feb 27 '17 at 19:51
  • mmm Do you know if I can simply test if there is a WIFI local connection ? For example if the url 192.168.0.100 is reachable. I tried with the Reachability without success. It tells me that it is connected, but it is not true. – ΩlostA Feb 27 '17 at 20:12
  • That's beyond the question. Please ask a new specific one. – vadian Feb 27 '17 at 20:16
  • Ok I will do it – ΩlostA Feb 27 '17 at 20:22
  • I did the ticket here for info : http://stackoverflow.com/questions/42495080/how-to-simply-test-a-local-wifi-connection-to-an-url-for-example-192-168-0-100?noredirect=1#comment72129547_42495080 – ΩlostA Feb 27 '17 at 20:39