iOS Question Will it be accepted in Apple Store?

Alex_197

Active Member
Licensed User
Hi all.
I've been working on my project that requires the app to run the timer when the app is in Inactive mode - when event App_Inactive fired.

Please see my main module code. I also attached my project.

Timer:
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 Timer1 As Timer
    Private strTimeFormatShow As String="hh:mm a"  
    Private strDateFormat As String="MM/dd/yyyy"
    Private Flag As Boolean=False
    Private Counter As Int
    Private txtLog As TextView
    Private lblVer As Label
    Private btnSave As Button
End Sub

Private Sub Application_Start (Nav As NavigationController)
    NavControl = Nav
    Page1.Initialize("Page1")
    Page1.RootPanel.LoadLayout("Page1")
    NavControl.ShowPage(Page1)
   
    DateTime.DateFormat=strDateFormat
    DateTime.TimeFormat=strTimeFormatShow
    lblVer.Text="01.01 11/21/2020"
       
    Timer1.Initialize("Timer1", 60000)
    Timer1.Enabled=True
   
    ReadLog
   
    Dim DeviceID As String=""
   
    DeviceID="DeviceID=" & GetVendorIdentifier
   
    lblVer.Text=lblVer.Text & CRLF &  DeviceID
   
   
End Sub


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



Private Sub Application_Background
   
    Flag=True
    Log("We are in background" & TAB & Today & TAB & Now & CRLF & "Timer is ticking")
    ShowLog("We are in background" & TAB & Today & TAB & Now & CRLF & "Timer is ticking")
   
End Sub

Private Sub Application_Inactive
    Flag=True
    Log("We are Inactive" & TAB & Today & TAB & Now)
    ShowLog("We are Inactive" & TAB & Today & TAB & Now)
End Sub

Private Sub Application_Active
    Flag=False
    Log("We are active" & TAB & Today & TAB & Now)
    ShowLog("We are active" & TAB & Today & TAB & Now)
   
End Sub

private Sub Application_Foreground
    Flag=False
    Log("Application_Foreground" & TAB & Today & TAB & Now )
    ShowLog("Application_Foreground" & TAB & Today & TAB & Now )
End Sub

Private Sub Timer1_Tick
    Log("Now=" & Now)
    RunCounter
End Sub


Private Sub RunCounter
   
    Log("Flag=" & Flag & TAB & Now)
    ShowLog("Flag=" & Flag & TAB & Now)
   
    If Flag=False Then
        Counter=0
        Return
    End If
   
    Counter=Counter+1
   
    Log(Now & TAB & "Counter=" & Counter)
    ShowLog(Now & TAB & "Counter=" & Counter)
   
    If Counter=5 Then
        Log("Time has expired")
        ShowLog("Time has expired")
        Counter=0
    End If
   
End Sub

private Sub Today As String
   
    Return DateTime.Date(DateTime.Now)
   
End Sub


private  Sub Now As String
   
    Return DateTime.Time(DateTime.Now)
   
End Sub


Private Sub ShowLog(str As String)
    Try
       
        txtLog.Text=txtLog.Text &  CRLF & str  
       
    Catch
        Log("ShowLog " & LastException)
    End Try
   
   
End Sub

Private Sub ReadLog
    Try
        If File.Exists(File.DirDocuments,"log.txt") Then
            Log("Exists")
            Dim str As String
            str=File.ReadString(File.DirDocuments,"log.txt")
            Log("ReadLog " & str)
        Else
            Log("Doesn't Exist")
        End If
    Catch
        Log(LastException)
    End Try
End Sub

Sub btnSave_Click
    Try
        Dim out As OutputStream
   
        out=File.OpenOutput(File.DirDocuments,"log.txt",True)
           
        File.WriteString(File.DirDocuments,"log.txt",txtLog.Text)
       
        out.Close
        Msgbox("Saved","Test")
    Catch
        Log(LastException)
    End Try
End Sub

Sub GetVendorIdentifier As String
    Dim no As NativeObject
    no = no.Initialize("UIDevice").RunMethod("currentDevice", Null)
    Dim id As Object = no.GetField("identifierForVendor").GetField("UUIDString")
    Return id
End Sub


Thank you.
 

Attachments

Last edited:

ilan

Expert
Licensed User
why do you need a timer?

you count each minute +1 so you can save the date when the app goes to background and then check the time when the app is again in the foreground and calculate the time difference and like this know the counting.

use the timer only when the app is in the foreground.

something like this:

B4X:
Private Sub Application_Inactive
'    Flag=False
'    Log("We are active" & TAB & Today & TAB & Now)
'    ShowLog("We are active" & TAB & Today & TAB & Now)
       File.WriteString(File.DirDocuments,"date.txt",DateTime.Now)
End Sub

Private Sub Application_Active
'    Flag=True
'    Log("We are Inactive" & TAB & Today & TAB & Now)
'    ShowLog("We are Inactive" & TAB & Today & TAB & Now)
    Dim exitDate As Long = File.ReadString(File.DirDocuments,"date.txt")
    Dim countingWhenAppWasInBackground As Int = Floor((DateTime.Now-exitDate)/60000)
    Log($"Counter: ${countingWhenAppWasInBackground}"$
End Sub
btw why are you setting an OutputStream but dont do anything whit it? if you use File.WriteString("") you dont need OutputStream.
 
Top