Ploetzeneder Ploetzeneder - 4 months ago 38
iOS Question

How to set timeout for dataWithContentsOfURL:url

I would like to download with a shorter timeout, so that it is faster, and to prevent the app from crashing on a bad connection.

- (void) CreateTitleView {
NSURL* url;
NSData* imageData;
imageData = [NSData dataWithContentsOfURL:url ];
UIImage* image = [UIImage imageWithData:imageData];

I am not good in objective C, so I ask for your help, to do this. Thanks.

Answer Source

You cannot control the download speed by setting a timeout. That would only control how long your application waited before giving up on the download. You should refactor your application to load the image data in the background, so that the UI remains responsive till the download is complete.

Check out NSURLConnection (sendAsynchronousRequest), or AFNetworking.