iOS Question How download file with resumable feature?

webhost.company

Active Member
Licensed User
Longtime User
I cannot resume a paused downloading file with OkHttp
Also I use below code but cannot resume downloading

B4X:
#if b4i
#If OBJC

NSURLResponse *DownloadedResponse;
NSMutableData *DownloadedData;
NSString *FilePath;
NSURLConnection * connection2;
NSURLRequest *request;

- (void) cancel: (bool) on {
    [connection2 cancel];
}

-(void)Download: (NSString *)FileUrl :(UILabel *)LabelToSet :(NSString *)Directory :(NSString *)FileName :(NSArray *)Headers :(NSArray *)Values
{

    FilePath = [Directory stringByAppendingString: FileName];

    DownloadedData = [[NSMutableData alloc] init];
    NSURL *url = [NSURL URLWithString:FileUrl];
    request = [NSURLRequest requestWithURL:url];
    NSMutableURLRequest *mutableRequest = [request mutableCopy];

    NSUInteger count = [Headers count];
    for (NSUInteger i = 0; i < count; i++) {
        NSString * key = [Headers objectAtIndex: i];
           NSString * val = [Values objectAtIndex: i];
        [mutableRequest addValue:val forHTTPHeaderField:key];
    }
    
    request = [mutableRequest copy];
    NSURLConnection * connection = [[NSURLConnection alloc] initWithRequest:request delegate:self startImmediately:YES];
    connection2    =    connection;
  
}

- (void)connection:(NSURLConnection *)connection didReceiveData:(NSData *)data
{
   [DownloadedData appendData:data];
   float res = ((100.0/DownloadedResponse.expectedContentLength)*DownloadedData.length)/100;

   NSString * LabelText = [NSString stringWithFormat:@"%f", (res)];
   NSString * LabelText2 = [NSString stringWithFormat:@"%d", [DownloadedData length]];
  
   [self.bi raiseEvent:nil event:@"download_progress::" params:@[(LabelText),LabelText2]];
}

- (void)connectionDidFinishLoading:(NSURLConnection *)connection
{
   [DownloadedData writeToFile:FilePath atomically:YES];
   [self.bi raiseEvent:nil event:@"download_complete" params:@[]];
}

- (void)connection:(NSURLConnection *)connection didFailWithError:(NSError *)error
{
   [self.bi raiseEvent:nil event:@"download_error:" params:@[error.localizedDescription]];
}

-(void)connection:(NSURLConnection *)connection didReceiveResponse:(NSURLResponse *)response {
  DownloadedResponse = response;
}
#End If
In hug file, that is necessary to resume the downloading
 

Semen Matusovskiy

Well-Known Member
Licensed User
Why do you want to pause and resume manuaĺy ? It' s possible to use background urlsession - it works in foreground also and resumes automatically
 
Upvote 0

Semen Matusovskiy

Well-Known Member
Licensed User
I have no idea, what does your code.
My suggestion - to use as usual httpjob.Download and similar. But to change NSUrlSession, for example, in Application_Start.

Attached sample ... Press a button and switch to another app. Download will be done even in background. IOS selects a 'good' time, so a downloadload in background may take some additional time.
 

Attachments

  • s02.zip
    3.7 KB · Views: 107
Upvote 0

aeric

Expert
Licensed User
Longtime User
 
Upvote 0
Top