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