How to set timeout for dataWithContentsOfURL: url

I would like to download with a shorter timeout so that it is faster and to prevent application crashes in a bad connection.

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

I do not know how in the C lens, so I ask you for help to do this. Thank.

+5
source share
2 answers

You cannot control the download speed by setting a timeout. This will only control the latency of your application before you refuse to download. You must reorganize your application to download image data in the background so that the user interface remains responsive until the download is complete.

NSURLConnection (sendAsynchronousRequest) AFNetworking.

+2

. API :

NSURLResponse* urlResponse;
NSError* error;
NSMutableURLRequest* urlRequest = [NSMutableURLRequest requestWithURL:url cachePolicy:NSURLRequestReloadIgnoringCacheData timeoutInterval:20];
NSData* d = [NSURLConnection sendSynchronousRequest:urlRequest returningResponse:&urlResponse error:&error];
+10

All Articles