Search code examples
iosobjective-cafnetworking-3

Afnetworking 3.0 total Bytes Expected To Write return -1


i want to configure progress bar but my url can not give me total size of my image . i follow this tutorial http://tarikfayad.com/afnetworking-downloading-files-with-progress/

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

//Most URLs I come across are in string format so to convert them into an NSURL and then instantiate the actual request
NSURL *formattedURL = [NSURL URLWithString:@"http://www.wallpapers13.com/wp-content/uploads/2016/04/Sunset-Large-Chinese-wall-Desktop-Wallpaper-HD-5250x3344.jpg"];
NSURLRequest *request = [NSURLRequest requestWithURL:formattedURL];

[manager setDownloadTaskDidWriteDataBlock:^(NSURLSession * _Nonnull session, NSURLSessionDownloadTask * _Nonnull downloadTask, int64_t bytesWritten, int64_t totalBytesWritten, int64_t totalBytesExpectedToWrite) {
    //CGFloat w = totalBytesWritten;
    CGFloat t = totalBytesExpectedToWrite;
    //CGFloat percentageCompleted = w/t;
    NSLog(@"%f",t); // Error retun -1
}];
NSURLSessionDownloadTask *download = [manager downloadTaskWithRequest:request progress:nil destination:^NSURL * _Nonnull(NSURL * _Nonnull targetPath, NSURLResponse * _Nonnull response) {
    //
    //Getting the path of the document directory
    NSURL *documentsDirectoryURL = [[NSFileManager defaultManager] URLForDirectory:NSDocumentDirectory inDomain:NSUserDomainMask appropriateForURL:nil create:NO error:nil];
    NSURL *fullURL = [documentsDirectoryURL URLByAppendingPathComponent:@"image1.jpg"];
    //If we already have a video file saved, remove it from the phone
    [self removeVideoAtPath:fullURL];
    return fullURL;
} completionHandler:^(NSURLResponse * _Nonnull response, NSURL * _Nullable filePath, NSError * _Nullable error) {
    if (!error) {
        //If there's no error, return the completion block
        [self completionBlock:(NSString *)filePath];
    } else {
        //Otherwise return the error block
        [self errorBlock:error];
    }
}];
[download resume];

Solution

  • Try this:

    NSURLSessionConfiguration * configuration = [NSURLSessionConfiguration defaultSessionConfiguration];
    AFURLSessionManager * manager = [[AFURLSessionManager alloc] initWithSessionConfiguration:configuration];
    
    NSURL * URL = [NSURL URLWithString:@"http://static1.visitfinland.com/wp-content/uploads/Header_Linnunlaulu_ruska_autumn.jpg"];
    NSURLRequest * request = [NSURLRequest requestWithURL:URL];
    
    NSURLSessionDownloadTask * downloadTask = [manager downloadTaskWithRequest:request
                                                                      progress:^(NSProgress * _Nonnull downloadProgress) {
        dispatch_async(dispatch_get_main_queue(), ^{
            // here you will be able to set progress to your progress view
            [yourProgressView setProgress:downloadProgress.fractionCompleted];
        });
    } 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) {
    
    }];
    
    [downloadTask resume];