iOS Code Snippet Monitor http requests progress

Erel

Administrator
Staff member
Licensed User
Code:
B4X:
Sub MonitorProgress
   If App.OSVersion < 11 Then Return
   Dim no As NativeObject = Me
   Dim hc As NativeObject = no.GetField("_httputils2service").GetField("_hc")
   Do While hc.IsInitialized = False Or hc.GetField("session").IsInitialized = False
       Dim hc As NativeObject = no.GetField("_httputils2service").GetField("_hc")
       Sleep(10)
   Loop
   
   Dim FirstNonEmpty As Boolean
   Do While True
       no.RunMethod("getTasks:", Array(hc.GetField("session")))
       Wait For Http_Tasks (oTasks As Object)
       Sleep(20)
       Dim tasks As List = oTasks
       If tasks.Size > 0 Then
           FirstNonEmpty = True
       Else
           If FirstNonEmpty Then Exit
       End If
       For Each task As NativeObject In tasks
           Dim url As String = task.GetField("originalRequest").GetField("URL").AsString
           Dim progress As NativeObject = task.GetField("progress")
           Dim fraction As Float = progress.GetField("fractionCompleted").AsNumber
           'work with progress value:
           Page1.Title = $"$1.2{fraction * 100}%-${url}"$
           Log(Page1.Title)
           '***********
       Next
   Loop
End Sub


#if OBJC
- (void) getTasks:(NSURLSession*)session {
   [session getAllTasksWithCompletionHandler:^(NSArray* tasks) {
       [self.bi raiseUIEvent:nil event:@"http_tasks:" params:@[tasks]];
   }];
}
#End If
Call MonitorProgress after you call HttpJob.Download or one of the other HttpJob methods.
On iOS 11+ devices it will track the task progress.

Example is attached.
 

Attachments

Top