iOS - Download file only if modified (NSURL & NSData)

后端 未结 2 1647
长情又很酷
长情又很酷 2021-01-14 10:54

I am downloading a bunch of image files from a server, and I want to ensure that they are downloaded only if they are newer. This method currently downloads the images just

2条回答
  •  情深已故
    2021-01-14 11:24

    If your server supports HTTP caching you can specify you want cached content with NSURLRequestReloadRevalidatingCacheData:

    NSURLRequest* request = [NSURLRequest requestWithURL:thumbnailURL cachePolicy:NSURLRequestReloadRevalidatingCacheData timeoutInterval:20];
    NSURLResponse* response;
    NSError* error;
    NSData* data = [NSURLConnection sendSynchronousRequest:request returningResponse:&response error:&error];
    UIImage* image = [UIImage imageWithData:data];
    

    For more info read the NSURLRequest documentation.

提交回复
热议问题