Created
November 10, 2015 20:25
-
-
Save eddieespinal/70a31a0dbc788db0bcda to your computer and use it in GitHub Desktop.
**Use this function below to check whether file exists at specified url**
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
**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