2017-02-11 22 views
0

ユーザーがボタンを押したときにurlから1つのファイルをダウンロードする必要があります。ユーザーがすぐに別のボタンを押すと、最初のファイルの完了後にそのファイルをダウンロードする必要があります.3番目のボタンを押すと、 AFNetworkingでこれを達成するには? ここで私のコードは、ファイルをダウンロードするサンプルです。事前にありがとうございます。AFNetworking urlからダウンロード

AFHTTPRequestOperationManager *manager = [AFHTTPRequestOperationManager manager]; 

manager.responseSerializer = [AFCompoundResponseSerializer serializer]; 
manager.responseSerializer.acceptableContentTypes = [NSSet setWithObjects:@"application/octet-stream",@"video/3gpp",@"audio/mp4",nil]; 

AFHTTPRequestOperation *operation = [manager GET:[array objectAtIndex:0] parameters:nil success:^(AFHTTPRequestOperation *operation, id responseObject) { 
    if (responseObject) { 

     NSData *data=[[NSData alloc] initWithData:responseObject]; 

     NSLog(@"Download Succesfully Completed"); 

     //after completion I'm implementing my method here 

     [self sentMsgSaveWithData:data orUrl:@"" withBool:YES withMsg_ID:@"" withDict:tempDict]; 


    } else { 
     NSLog(@"Download Error"); 

    } 
} failure:^(AFHTTPRequestOperation *operation, NSError *error) { 
    NSLog(@"Download Error %@",error.localizedDescription); 


}]; 

[operation start]; 
+0

あなたはときに、ユーザーの押しボタンのダウンロードの要求を管理する必要が.ANDバックグラウンドであなたはそれのための同期を使用することができますステップ –

+0

によってダウンロードステップを持っています。それを探してください。 –

+0

@ Himanshu Moradiya:どのようにリクエストを処理するのですか? –

答えて

0

デフォルトの設定でセッションを試してみてください:

NSURLSessionConfiguration *configuration = [NSURLSessionConfiguration defaultSessionConfiguration]; 
    AFURLSessionManager *manager = [[AFURLSessionManager alloc] initWithSessionConfiguration:configuration]; 

    NSURL *URL = [NSURL URLWithString:@"http://example.com/download.zip"]; 
    NSURLRequest *request = [NSURLRequest requestWithURL:URL]; 

    NSURLSessionDownloadTask *downloadTask = [manager downloadTaskWithRequest:request progress:nil destination:^NSURL *(NSURL *targetPath, NSURLResponse *response) { 

NSURL *documentsDirectoryURL = [[NSFileManager defaultManager] URLForDirectory:NSDocumentDirectory inDomain:NSUserDomainMask appropriateForURL:nil create:NO error:nil]; 
     return [documentsDirectoryURL URLByAppendingPathComponent:[response suggestedFilename]]; 
    } completionHandler:^(NSURLResponse *response, NSURL *filePath, NSError *error) { 
     NSLog(@"File downloaded to: %@", filePath); 
    }]; 
    [downloadTask resume]; 
+0

私はファイルデータのみを必要とします。ファイルデータを取得する方法は? –

+0

あなたのファイルはそこからfilePathにダウンしています。あなたのファイルで何をしたいのですか? – Aragunz

+0

しかし、私はそれぞれのボタンのプレスでひとつずつダウンロードしたいと思います。私はNSOperationQueueと一緒に行く必要がありますか?最初のボタンをクリックした後、ユーザーが2番目のファイルをクリックすると、両方のファイルをダウンロードする必要がありますが、それぞれのボタンをクリックすると、ありがとうございました。 –

関連する問題