Search code examples
objective-ciosnsdatahttp-status-code-302

dataWithContentsOfURL and HTTP 302 redirects


How does [NSData dataWithContentsOfURL:] deal with 302 redirects?

I am requesting an MP3 file from a server by passing the URL of a PHP page which will check the user-agent and redirect to the MP3 file on a different server.


Solution

  • Never use [NSData dataWithContentsOfURL:] in a real app. It makes a synchronous network connection and your app will be killed by the OS if that doesn't finish quickly enough.

    Stick to NSURLConnectionDataDelegate or use [NSURLConnection sendAsynchronousRequest:queue:completionHandler:.