iOS Question Lack of services in ios

red30

Well-Known Member
Licensed User
Longtime User
I wrote an application for Android and used services there for compressing photos and uploading photos to the server.
Compressing a photo seems like a long enough time, especially if it is 100 photos, so I implemented it in the service so that at this moment the user can minimize the application and do other things. Can I do something like this in ios?
B4X:
    For i=0 To uris.Size-1
        Dim FileSize=100 As Int
        Do While File.Size(File.DirInternal,NumberFile&".jpg")>500000
            Out = File.OpenOutput(File.DirInternal,NumberFile&".jpg",False)
            Bitmap.WriteToStream(Out, FileSize, "JPEG")
            Out.Close
            FileSize=FileSize-10
        Loop
    Next
Then I upload these photos to the server using web services (OkHttp). When sending 100 photos, this is also a rather lengthy process, so it was implemented using services. Can I do something like this in ios?
 

jahswant

Well-Known Member
Licensed User
Longtime User
Create a regular class and implement your logic in it. But You won’t be able to run it in the background like a service. Your app need to be running while the class operates.
 
Upvote 0

Erel

B4X founder
Staff member
Licensed User
Longtime User
The best you can do is:
1. Ask the OS to give you more time.
2. Notify the user that the app needs to be visible in order to complete uploading data.

1:
Example of requesting the OS to let your app complete the task. Test it in release mode and switch to the background after you click the button. You will see that it will run for about 30 seconds. This is a bit more than the few seconds that you will have without calling StartBackgroundTask.
Note that in debug mode, the debugger makes a similar request when the app moves to the background.
B4X:
Sub Process_Globals
    'These global variables will be declared once when the application starts.
    'Public variables can be accessed from all modules.
    Public App As Application
    Public NavControl As NavigationController
    Private Page1 As Page
    Private xui As XUI
End Sub

Private Sub Application_Start (Nav As NavigationController)
    NavControl = Nav
    Page1.Initialize("Page1")
    Page1.RootPanel.LoadLayout("Page1")
    NavControl.ShowPage(Page1)
    App.RegisterUserNotifications(True, True, True)
End Sub

Sub Button1_Click
    StartBackgroundTask
    Dim no As NativeObject = App
    Log("move to the background")
    Sleep(5000)
    Log($"Remaining time: $1.2{no.GetField("backgroundTimeRemaining").AsNumber}"$)
    App.ApplicationIconBadgeNumber = Min(1000, no.GetField("backgroundTimeRemaining").AsNumber + 100)
    Do While True
        Log(DateTime.Now)
        App.ApplicationIconBadgeNumber = App.ApplicationIconBadgeNumber - 1
        Sleep(1000)
    Loop
End Sub

Private Sub Page1_Resize(Width As Int, Height As Int)
    
End Sub

Private Sub StartBackgroundTask
    Dim no As NativeObject = Me
    no.RunMethod("startBackgroundTask", Null)
End Sub


#if OBJC
- (void) startBackgroundTask {
 UIBackgroundTaskIdentifier bgTask = [[UIApplication sharedApplication] beginBackgroundTaskWithExpirationHandler:^{
            [[UIApplication sharedApplication] endBackgroundTask:bgTask];
        }];
}
#End If
 
Upvote 0

red30

Well-Known Member
Licensed User
Longtime User
Is there any way you can get iOS not to turn off the screen while the application is running?
In the Android, I did it with PhoneWakeState
B4X:
PWS.KeepAlive(True)
 
Upvote 0
Top