{"id":8009,"date":"2015-11-20T05:43:29","date_gmt":"2015-11-20T05:43:29","guid":{"rendered":"https:\/\/unknownerror.org\/index.php\/2015\/11\/20\/how-to-download-a-file-and-save-it-to-the-documents-directory-with-afnetworking-open-source-projects-afnetworking-afnetworking\/"},"modified":"2022-08-30T15:03:04","modified_gmt":"2022-08-30T15:03:04","slug":"how-to-download-a-file-and-save-it-to-the-documents-directory-with-afnetworking-open-source-projects-afnetworking-afnetworking","status":"publish","type":"post","link":"https:\/\/unknownerror.org\/index.php\/2015\/11\/20\/how-to-download-a-file-and-save-it-to-the-documents-directory-with-afnetworking-open-source-projects-afnetworking-afnetworking\/","title":{"rendered":"How to download a file and save it to the documents directory with AFNetworking?-open source projects AFNetworking\/AFNetworking"},"content":{"rendered":"<p><img decoding=\"async\" src=\"http:\/\/i.stack.imgur.com\/nIvic.jpg?s=128&amp;g=1\" \/> <strong>wzbozon<\/strong><\/p>\n<p>Yes, it is better to use <code>AFNetworking 2.0<\/code> way with <code>AFHTTPRequestOperationManager<\/code>. With old way my file did download but for some reason didn&#8217;t update in file system.<\/p>\n<p>Appending to <strong>swilliam&#8217;s<\/strong> answer, to show download progress, in <code>AFNetworking 2.0<\/code> you do similarly &#8211; just set download progress block after setting output stream.<\/p>\n<pre><code>__weak SettingsTableViewController *weakSelf = self;\n\noperation.outputStream = [NSOutputStream outputStreamToFileAtPath:newFilePath append:NO];\n\n[operation setDownloadProgressBlock:^(NSUInteger bytesWritten, long long totalBytesWritten, long long totalBytesExpectedToRead) {\n\n    float progress = totalBytesWritten \/ (float)totalBytesExpectedToRead;\n\n    NSString *progressMessage = [NSString stringWithFormat:@\"%@ \\n %.2f %% \\n %@ \/ %@\", @\"Downloading ...\", progress * 100, [weakSelf fileSizeStringWithSize:totalBytesWritten], [weakSelf fileSizeStringWithSize:totalBytesExpectedToRead]];\n\n    [SVProgressHUD showProgress:progress status:progressMessage];\n}];\n<\/code><\/pre>\n<p>This is my method to create bytes string:<\/p>\n<pre><code>- (NSString *)fileSizeStringWithSize:(long long)size\n{\n    NSString *sizeString;\n    CGFloat f;\n\n    if (size &lt; 1024) {\n        sizeString = [NSString stringWithFormat:@\"%d %@\", (int)size, @\"bytes\"];\n    }\n    else if ((size &gt;= 1024)&amp;&amp;(size &lt; (1024*1024))) {\n        f = size \/ 1024.0f;\n        sizeString = [NSString stringWithFormat:@\"%.0f %@\", f, @\"Kb\"];\n    }\n    else if (size &gt;= (1024*1024)) {\n        f = size \/ (1024.0f*1024.0f);\n        sizeString = [NSString stringWithFormat:@\"%.0f %@\", f, @\"Mb\"];\n    }\n\n    return sizeString;\n}\n<\/code><\/pre>\n","protected":false},"excerpt":{"rendered":"<p>wzbozon Yes, it is better to use AFNetworking 2.0 way with AFHTTPRequestOperationManager. With old way my file did download but for some reason didn&#8217;t update in file system. Appending to swilliam&#8217;s answer, to show download progress, in AFNetworking 2.0 you do similarly &#8211; just set download progress block after setting output stream. __weak SettingsTableViewController *weakSelf [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"closed","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[1],"tags":[],"class_list":["post-8009","post","type-post","status-publish","format-standard","hentry","category-uncategorized"],"_links":{"self":[{"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/posts\/8009","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/comments?post=8009"}],"version-history":[{"count":1,"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/posts\/8009\/revisions"}],"predecessor-version":[{"id":8701,"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/posts\/8009\/revisions\/8701"}],"wp:attachment":[{"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/media?parent=8009"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/categories?post=8009"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/tags?post=8009"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}