iOS Code Snippet Monitor http requests progress

Discussion in 'iOS Code Snippets' started by Erel, Apr 16, 2019.

Similar threads

B4i Tutorial Background Fetch (Downloads)
B4i Tutorial iBeacons
B4i Tutorial Notifications with actions
B4i Code Snippet WebView "extra"
B4i Question Change NavBar Text Color
  1. Erel

    Erel Administrator Staff Member Licensed User

    Code:
    Code:
    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.
     

    Attached Files:

Loading...
  1. This site uses cookies to help personalise content, tailor your experience and to keep you logged in if you register.
    By continuing to use this site, you are consenting to our use of cookies.
    Dismiss Notice