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

Check if NSURL returns 404

As you may know already that general error can capture by didFailWithError method: – (void)connection:(NSURLConnection *)connection didFailWithError:(NSError *)error { NSLog(@”Connection failed! Error – %@ %@”, [error localizedDescription], [[error userInfo] objectForKey:NSErrorFailingURLStringKey]); } but for 404 “Not Found” or 500 “Internal Server Error” should able to capture inside didReceiveResponse method: – (void)connection:(NSURLConnection *)connection didReceiveResponse:(NSURLResponse *)response { if … Read more

iPhone SDK: How do you download video files to the Document Directory and then play them?

Your code will work to play a movie file. The simplest way to download is synchronously: NSData *data = [NSData dataWithContentsOfURL:movieUrl]; [data writeToURL:movieUrl atomically:YES]; But it is better (for app responsiveness, etc) to download asynchronously: NSURLRequest *theRequest = [NSURLRequest requestWithURL:movieUrl cachePolicy:NSURLRequestReloadIgnoringLocalCacheData timeoutInterval:60]; receivedData = [[NSMutableData alloc] initWithLength:0]; NSURLConnection *connection = [[NSURLConnection alloc] initWithRequest:theRequest delegate:self startImmediately:YES]; … Read more

How to find size of a file before downloading it in iOS 7?

Make a request using the HEAD method. For example: NSMutableURLRequest *request = [NSMutableURLRequest requestWithURL:url]; [request setHTTPMethod:@”HEAD”]; This request will be identical to a GET but it won’t return the body. Then call long long size = [response expectedContentLength]; Complete example with NSURLConnection (works for NSURLSession too of course): NSURL *URL = [NSURL URLWithString:@”http://www.google.com”]; NSMutableURLRequest *request … Read more