Skip to content

Instantly share code, notes, and snippets.

@eddieespinal
Created November 10, 2015 20:25
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save eddieespinal/70a31a0dbc788db0bcda to your computer and use it in GitHub Desktop.
Save eddieespinal/70a31a0dbc788db0bcda to your computer and use it in GitHub Desktop.
**Use this function below to check whether file exists at specified url**
**Use this function below to check whether file exists at specified url**
+(void)checkWhetherFileExistsIn:(NSURL *)fileUrl Completion:(void (^)(BOOL success, NSString *fileSize ))completion
{
//MAKING A HEAD REQUEST
NSMutableURLRequest *request = [NSMutableURLRequest requestWithURL:fileUrl];
request.HTTPMethod = @"HEAD";
request.timeoutInterval = 3;
[NSURLConnection sendAsynchronousRequest:request queue:[NSOperationQueue currentQueue] completionHandler:^(NSURLResponse *response, NSData *data, NSError *connectionError)
{
NSHTTPURLResponse *httpResponse = (NSHTTPURLResponse *) response;
if (connectionError == nil) {
if ((long)[httpResponse statusCode] == 200)
{
//FILE EXISTS
NSDictionary *dic = httpResponse.allHeaderFields;
NSLog(@"Response 1 %@",[dic valueForKey:@"Content-Length"]);
completion(TRUE,[dic valueForKey:@"Content-Length"]);
}
else
{
//FILE DOESNT EXIST
NSLog(@"Response 2");
completion(FALSE,@"");
}
}
else
{
NSLog(@"Response 3");
completion(FALSE,@"");
}
}];
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment