iOS Code Snippet Msgbox with timeout

A Msgbox that disappears automatically after the set timeout. The result is "Cancel".
B4X:
Sub ShowMessageWithTimeout(text As String, timeout As Int) As ResumableSub
   Dim sf As Object = Msgbox2("Msg", text, "Title", Array ("Yes", "No", "Cancel"))
   Dim cancel(1) As Boolean
   CheckForTimeout(sf, timeout, cancel)
   Wait For Msg_Click (ButtonText As String)
   cancel(0) = True
   Return ButtonText
End Sub

Private Sub CheckForTimeout (sf As NativeObject, delay As Int, cancel() As Boolean)
   Dim n As Long = DateTime.Now + delay
   Do While DateTime.Now < n
       If cancel(0) = True Then Return
       Sleep(100)
   Loop
   sf.RunMethod("dismissWithClickedButtonIndex:animated:", Array(-1, True))
   CallSubDelayed2(Me, "Msg_Click", "Cancel")
End Sub

Usage example:
B4X:
Wait For (ShowMessageWithTimeout("testing???", 4000)) Complete  (Result As String)
Log(Result)
 
Top