9
9

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 5 years have passed since last update.

AFNetworkingでHTTP GETしてファイルに保存

Last updated at Posted at 2014-04-22
# import "AFHTTPRequestOperationManager.h"

-(void)downloadUrl:(NSString*)url toPath:(NSString*)path
          success :(void (^)(AFHTTPRequestOperation *operation, id responseObject))success
          failure :(void (^)(AFHTTPRequestOperation *operation, NSError *error   ))failure
          progress:(void (^)(NSUInteger bytesRead, long long totalBytesRead, long long totalBytesExpectedToRead))progress
{
    AFHTTPRequestOperationManager *manager = [AFHTTPRequestOperationManager manager];
    AFHTTPRequestOperation *op = [manager GET:url
                                  parameters:nil
                                  success:success
                                  failure:failure];
    [op setDownloadProgressBlock:progress];
    op.outputStream = [NSOutputStream outputStreamToFileAtPath:path append:NO];
}

    NSString *path = [NSTemporaryDirectory() stringByAppendingPathComponent:@"tmp.bin"];
    [self downloadUrl:url toPath:path
              success:^(AFHTTPRequestOperation *operation, id responseObject) {
                  NSLog(@"DL: success");
              }
              failure:^(AFHTTPRequestOperation *operation, NSError *error) {
                  NSLog(@"DL: failed %@", error);
              }
             progress:^(NSUInteger bytesRead, long long totalBytesRead, long long totalBytesExpectedToRead) {
                 NSLog(@"DL: progress %d", (int)(100 * totalBytesRead / totalBytesExpectedToRead));
             }];
9
9
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
9
9

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?