Skip to content

Instantly share code, notes, and snippets.

@eddieespinal
Created November 10, 2015 20:25
Show Gist options
  • Select an option

  • Save eddieespinal/70a31a0dbc788db0bcda to your computer and use it in GitHub Desktop.

Select an option

Save eddieespinal/70a31a0dbc788db0bcda to your computer and use it in GitHub Desktop.

Revisions

  1. eddieespinal created this gist Nov 10, 2015.
    36 changes: 36 additions & 0 deletions checkWhetherFileExistsIn
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,36 @@
    **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,@"");
    }

    }];
    }