1

I download a file from the server. while the file downloads, I want to show percent progress in table cell. How can I do this?

Download file code

NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
NSString *documentsDirectory = [paths objectAtIndex:0];
NSString *filePath = [documentsDirectory stringByAppendingPathComponent:@"01.mp3"];
dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^{
BOOL fileExists = [[NSFileManager defaultManager] fileExistsAtPath:filePath isDirectory:NO];

if (!fileExists) {

    NSString *stringURL = @"https://drive.google.com/uc?export=download&id=0B6zMam2kAK39X0FOaUJXVDUwOHc";
    NSURL  *url = [NSURL URLWithString:stringURL];
    NSData *urlData = [NSData dataWithContentsOfURL:url];
    [urlData writeToFile:filePath atomically:YES];

}
});
self.audioPlayer = [[AVAudioPlayer alloc] initWithContentsOfURL:[NSURL URLWithString:filePath] error:nil];
}
rmaddy
  • 314,917
  • 42
  • 532
  • 579
user
  • 53
  • 7

2 Answers2

0

That's not possible with [NSData dataWithContentsOfUrl]. You'll need a library for that such as AFNetworking

See How to get download progress in AFNetworking 2.0?

Community
  • 1
  • 1
ByteWelder
  • 5,464
  • 1
  • 38
  • 45
0

Create Custom Cell XIB file add Progress bar on it

enter image description here

Start download in your view controller, and access progress bar and set progress in didReceiveData method

I just created in view controller, you need to use it with table view methods

Code sample :

   @interface ViewController ()<NSURLConnectionDataDelegate>

    //Put these in custom cell file
    @property (weak, nonatomic) IBOutlet UIProgressView *progressView;
    @property (weak, nonatomic) IBOutlet UIImageView *imageView;

    @property (strong, nonatomic) NSURLConnection *connectionManager;
    @property (strong, nonatomic) NSMutableData *downloadedMutableData;
    @property (strong, nonatomic) NSURLResponse *urlResponse;

    @end

    @implementation ViewController

    - (void)viewDidLoad
    {
        [super viewDidLoad];

        //Start download request logic
        self.downloadedMutableData = [[NSMutableData alloc] init];
        NSURLRequest *urlRequest = [NSURLRequest requestWithURL:[NSURL URLWithString:@"https://www.planwallpaper.com/static/images/63906_1_BdhSun5.jpg"]
                                                    cachePolicy:NSURLRequestReloadIgnoringLocalCacheData
                                                timeoutInterval:60.0];
        self.connectionManager = [[NSURLConnection alloc] initWithRequest:urlRequest delegate:self];
    }



    #pragma mark - Delegate Methods
    -(void)connection:(NSURLConnection *)connection didReceiveResponse:(NSURLResponse *)response {
        NSLog(@"%lld", response.expectedContentLength);
        self.urlResponse = response;
    }

//Show progress download while receiving data 
    -(void)connection:(NSURLConnection *)connection didReceiveData:(NSData *)data {
        [self.downloadedMutableData appendData:data];
        self.progressView.progress = ((100.0/self.urlResponse.expectedContentLength)*self.downloadedMutableData.length)/100;
        if (self.progressView.progress == 1) {
            self.progressView.hidden = YES;
        } else {
            self.progressView.hidden = NO;
        }
        NSLog(@"%.0f%%", ((100.0/self.urlResponse.expectedContentLength)*self.downloadedMutableData.length));
    }

    -(void)connectionDidFinishLoading:(NSURLConnection *)connection {
        NSLog(@"Finished");
        self.imageView.image = [UIImage imageWithData:self.downloadedMutableData];
    }

Result looks like

enter image description here

swiftBoy
  • 35,607
  • 26
  • 136
  • 135
  • if I use `NSURLRequest *urlRequest = [NSURLRequest requestWithURL:[NSURL URLWithString:@"https://drive.google.com/uc?export=download&id=0B6zMam2kAK39Zm5LUTctZXNLUlE»]` Progress doesn’t work. Why? – user Apr 01 '16 at 15:27
  • see **we need a delegate method where we can get our download progress** so i used **NSURLConnection** class because it has **didReceiveData** delegate method where i am updating progress bar – swiftBoy Apr 01 '16 at 15:31