iOS Question Huge Download with Progress

Pendrush

Well-Known Member
Licensed User
Longtime User
Not related to question, just to warn you. Every app that download or convert videos from YouTube is forbidden on app store.

  • 8.6
    Apps that include the ability to download music or video content from third party sources (e.g. YouTube, SoundCloud, Vimeo, etc) without explicit authorization from those sources will be rejected
More on link: https://developer.apple.com/app-store/review/guidelines/
 
Upvote 0

JanPRO

Well-Known Member
Licensed User
Longtime User
Hi,

use NSURLConnection.

Here is an little example:

I want to load this 4k image into an ImageView:
B4X:
Sub Download_Complete
Log("File was successfully downloaded")

Dim BM As Bitmap
BM.Initialize(File.DirDocuments,"Mountains.jpg")
ImageView1.Bitmap = BM
End Sub

Sub Download_Error(ErrorMessage As String)
Log(ErrorMessage)
End Sub

Sub Button1_Click
Dim NativeMe As NativeObject = Me
NativeMe.RunMethod("Download:::::",Array("http://data.hdwallpapers.im/nature_mountain_4k.jpg",ProgressView1,Label1,File.DirDocuments,"Mountains.jpg"))
End Sub


#If OBJC

UIProgressView * ProgressBar;

NSURLResponse *DownloadedResponse;
NSMutableData *DownloadedData;
NSString *FilePath;
UILabel *ProgressLabel;

-(void)Download: (NSString *)FileUrl :(UIProgressView *)ProgressBarToSet :(UILabel *)LabelToSet :(NSString *)Directory :(NSString *)FileName
{
   ProgressBar = ProgressBarToSet;
   ProgressBar.progress = 0;

   ProgressLabel = LabelToSet;

   FilePath = [Directory stringByAppendingString: FileName];

   DownloadedData = [[NSMutableData alloc] init];
   NSURL *url = [NSURL URLWithString:FileUrl];
   NSURLRequest *request = [NSURLRequest requestWithURL:url];
   NSURLConnection * connection = [[NSURLConnection alloc] initWithRequest:request delegate:self startImmediately:YES];
}

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

   NSString * LabelText = [[NSString stringWithFormat:@"%0.f", (ProgressBar.progress * 100)] stringByAppendingString:@"%"];
   ProgressLabel.text = LabelText;
}

- (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

based on: http://www.devfright.com/ios-data-download-progress-bar-tutorial/
 
Last edited:
Upvote 0

Alberto Iglesias

Well-Known Member
Licensed User
Longtime User
JanPro,

This code work like a charm!


THANK YOUUUUUUUUUUUUUUUUUUUUUUU!!!


look:

youdown.jpg
 
Upvote 0

chjk

Member
Licensed User
Longtime User
Hi,

use NSURLConnection.

Here is an little example:

I want to load this 4k image into an ImageView:
B4X:
Sub Download_Complete
Log("File was successfully downloaded")

Dim BM As Bitmap
BM.Initialize(File.DirDocuments,"Mountains.jpg")
ImageView1.Bitmap = BM
End Sub

Sub Download_Error(ErrorMessage As String)
Log(ErrorMessage)
End Sub

Sub Button1_Click
Dim NativeMe As NativeObject = Me
NativeMe.RunMethod("Download:::::",Array("http://data.hdwallpapers.im/nature_mountain_4k.jpg",ProgressView1,Label1,File.DirDocuments,"Mountains.jpg"))
End Sub


#If OBJC

UIProgressView * ProgressBar;

NSURLResponse *DownloadedResponse;
NSMutableData *DownloadedData;
NSString *FilePath;
UILabel *ProgressLabel;

-(void)Download: (NSString *)FileUrl :(UIProgressView *)ProgressBarToSet :(UILabel *)LabelToSet :(NSString *)Directory :(NSString *)FileName
{
   ProgressBar = ProgressBarToSet;
   ProgressBar.progress = 0;

   ProgressLabel = LabelToSet;

   FilePath = [Directory stringByAppendingString: FileName];

   DownloadedData = [[NSMutableData alloc] init];
   NSURL *url = [NSURL URLWithString:FileUrl];
   NSURLRequest *request = [NSURLRequest requestWithURL:url];
   NSURLConnection * connection = [[NSURLConnection alloc] initWithRequest:request delegate:self startImmediately:YES];
}

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

   NSString * LabelText = [[NSString stringWithFormat:@"%0.f", (ProgressBar.progress * 100)] stringByAppendingString:@"%"];
   ProgressLabel.text = LabelText;
}

- (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

based on: http://www.devfright.com/ios-data-download-progress-bar-tutorial/
It's very good! Do you know ftp how to download or upload file show process?
 
Upvote 0

cloner7801

Active Member
Licensed User
Longtime User
Hi,

use NSURLConnection.

Here is an little example:

I want to load this 4k image into an ImageView:
B4X:
Sub Download_Complete
Log("File was successfully downloaded")

Dim BM As Bitmap
BM.Initialize(File.DirDocuments,"Mountains.jpg")
ImageView1.Bitmap = BM
End Sub

Sub Download_Error(ErrorMessage As String)
Log(ErrorMessage)
End Sub

Sub Button1_Click
Dim NativeMe As NativeObject = Me
NativeMe.RunMethod("Download:::::",Array("http://data.hdwallpapers.im/nature_mountain_4k.jpg",ProgressView1,Label1,File.DirDocuments,"Mountains.jpg"))
End Sub


#If OBJC

UIProgressView * ProgressBar;

NSURLResponse *DownloadedResponse;
NSMutableData *DownloadedData;
NSString *FilePath;
UILabel *ProgressLabel;

-(void)Download: (NSString *)FileUrl :(UIProgressView *)ProgressBarToSet :(UILabel *)LabelToSet :(NSString *)Directory :(NSString *)FileName
{
   ProgressBar = ProgressBarToSet;
   ProgressBar.progress = 0;

   ProgressLabel = LabelToSet;

   FilePath = [Directory stringByAppendingString: FileName];

   DownloadedData = [[NSMutableData alloc] init];
   NSURL *url = [NSURL URLWithString:FileUrl];
   NSURLRequest *request = [NSURLRequest requestWithURL:url];
   NSURLConnection * connection = [[NSURLConnection alloc] initWithRequest:request delegate:self startImmediately:YES];
}

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

   NSString * LabelText = [[NSString stringWithFormat:@"%0.f", (ProgressBar.progress * 100)] stringByAppendingString:@"%"];
   ProgressLabel.text = LabelText;
}

- (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

based on: http://www.devfright.com/ios-data-download-progress-bar-tutorial/
How can we show connection speed ?
 
Upvote 0

schimanski

Well-Known Member
Licensed User
Longtime User
Is it possible to use this method with parameters linke Download2?

B4X:
job.Download2("http://www.example.com", _
    Array As String("key1", "value1", "key2", "value2"))

I have to send the access data to the server.

Thanks for help...
 
Upvote 0

Alberto Iglesias

Well-Known Member
Licensed User
Longtime User
BUt the code is already here:


The sample in the first post is:
B4X:
Dim NativeMe As NativeObject = Me
NativeMe.RunMethod("Download:::::",Array("http://data.hdwallpapers.im/nature_mountain_4k.jpg",ProgressView1,Label1,File.DirDocuments,"Mountains.jpg"))

is just to change for get the .mp4 files
B4X:
NativeMe.RunMethod("Download:::::",Array(strURL,prvDown,lblProgress,File.DirTemp, "videotmp.mp4"))

and to get the mp4 URLS from youtube, you create the libraryiYoutubeParser
 

Attachments

  • iYoutubeParser.zip
    88.1 KB · Views: 275
Upvote 0
Top