iOS: How can i receive HTTP 401 instead of -1012 NSURLErrorUserCancelledAuthentication

Yes. Stop using the synchronous API. If you use the asynchronous delegate-based API then you have a lot more control over the connection. With this API, except in cases where an error is encountered before the HTTP header is received, you will always receive -connection:didReceiveResponse:, which gives you access to the HTTP header fields (encapsulated … Read more

Ignoring certificate errors with NSURLConnection

You could simply ignore the invalid certificate if you are not sending any sensitive information. This article describes how you could do that. Here is an example implementation by Alexandre Colucci for one of the methods described in that article. Essentially you want to define a dummy interface just above the @implementation: @interface NSURLRequest (DummyInterface) … Read more

NSMutableURLRequest not obeying my timeoutInterval

There’s a thread on Apple dev forums discussing this issue. Apparently on iPhone OS, the setter mandates timeoutInterval a minimum of 240 seconds (4 minutes). This only occurs when the postBody is not empty (typically when using a POST request). This seems crazy, but apparently it’s there to make sure requests leave the system even … Read more

NSURLConnection and grand central dispatch

I recommend you to see WWDC Sessions about network application in iPhone OS. WWDC 2010 Session 207 – Network Apps for iPhone OS, Part 1 WWDC 2010 Session 208 – Network Apps for iPhone OS, Part 2 The lecturer said “Threads Are Evilâ„¢” for network programming and recommended to use asynchronous network programming with RunLoop. … Read more

NSURLConnection sendAsynchronousRequest:queue:completionHandler: making multiple requests in a row?

There’s lots of ways you can do this depending on the behavior you want. You can send a bunch of asynchronous requests at once, track the number of requests that have been completed, and do something once they’re all done: NSInteger outstandingRequests = [requestsArray count]; for (NSURLRequest *request in requestsArray) { [NSURLConnection sendAsynchronousRequest:request queue:[NSOperationQueue mainQueue] … Read more

How to send Asynchronous URL Request?

you can use NSURLConnection class NSURLRequest *request = [NSURLRequest requestWithURL:[NSURL URLWithString:url]]; [[[NSURLConnection alloc] initWithRequest:request delegate:self] autorelease]; and handle its response and errors using its delegate methods. – (void)connection:(NSURLConnection *)connection didReceiveResponse:(NSURLResponse *)response – (void)connection:(NSURLConnection *)connection didReceiveData:(NSData *)data – (void)connection:(NSURLConnection *)connection didFailWithError:(NSError *)error – (void)connectionDidFinishLoading:(NSURLConnection *)connection You can find implementation of NSURLConnection Apple docs: Using NSURLConnection How … Read more

Post data in Objective C using Json

I think you would be better off using the NSJSONSerialization class like this: NSDictionary *tmp = [[NSDictionary alloc] initWithObjectsAndKeys: email, @”Email”, fname, @”FirstName”, nil]; NSError *error; NSData *postdata = [NSJSONSerialization dataWithJSONObject:tmp options:0 error:&error]; [request setHTTPBody:postData]; Using a dictionary and then converting it to JSON it’s easier than creating it like a string. Good luck! [SWIFT … Read more