Platforms to show: All Mac Windows Linux Cross-Platform

/MacFrameworks/NSNetService/NSNetService iOS/iOS Companion App


You find this example project in your Plugins Download as a Xojo project file within the examples folder: /MacFrameworks/NSNetService/NSNetService iOS/iOS Companion App

This example is the version from Sun, 13th May 2023.

Project "iOS Companion App.xojo_binary_project"
Class App Inherits MobileApplication
End Class
Class MainScreen Inherits MobileScreen
Control SendButton Inherits MobileButton
ControlInstance SendButton Inherits MobileButton
Constraint Constraint 1
Constraint Constraint 2
Constraint Constraint 3
Constraint Constraint 4
EventHandler Sub Pressed() If Socket <> Nil Then Dim j As New JSONItem j.Value("Message") = MessageField.Text Socket.Send j End If End EventHandler
End Control
Control StatusLabel Inherits MobileLabel
ControlInstance StatusLabel Inherits MobileLabel
Constraint Constraint 1
Constraint Constraint 2
Constraint Constraint 3
Constraint Constraint 4
End Control
Control MessageField Inherits MobileTextField
ControlInstance MessageField Inherits MobileTextField
Constraint Constraint 1
Constraint Constraint 2
Constraint Constraint 3
Constraint Constraint 4
End Control
Control SendButton1 Inherits MobileButton
ControlInstance SendButton1 Inherits MobileButton
Constraint Constraint 1
Constraint Constraint 2
Constraint Constraint 3
Constraint Constraint 4
EventHandler Sub Pressed() If Socket <> Nil Then Dim j As New JSONItem j.Value("Message") = MessageField.Text Socket.Send j End If End EventHandler
End Control
EventHandler Sub Opening() StartBrowser End EventHandler
Sub FoundService(m as NetworkService) If socket = Nil Then // for now ignore second one found FoundService = m socket = New MySocket socket.Address = m.hostName Socket.Port = m.port Socket.MainScreen = Me System.DebugLog "Connect to "+m.hostName+":"+m.port.ToString StatusLabel.Text = "Connecting..." Socket.Connect End If End Sub
Sub ServiceConnect(j as JSONItem) Dim ServiceName As String = j.Value("ServiceName") SendButton.Enabled = True StatusLabel.Text = "Connected to "+ServiceName SendButton.Enabled = True End Sub
Sub SocketLost() // let's just start over StartBrowser End Sub
Sub StartBrowser() browser = New ServiceBrowser Const domain = "" // default Const type = "_test._tcp." browser.MainScreen = Self browser.searchForServicesOfType type, domain End Sub
Property FoundService As NetworkService
Property Socket As MySocket
Property browser As ServiceBrowser
End Class
Class LaunchScreen Inherits iosView
End Class
Class ServiceBrowser Inherits NSNetServiceBrowserMBS
EventHandler Sub DidFindService(service as NSNetServiceMBS, moreComing as Boolean) System.DebugLog CurrentMethodName // make a new object so we have our events installed dim m as new NetworkService(Service) // need to resolve to know IP and Port m.resolve m.MainScreen = Me.MainScreen services.Add m End EventHandler
EventHandler Sub DidNotSearch(error as Dictionary) System.DebugLog CurrentMethodName Break // error? End EventHandler
Property MainScreen As MainScreen
Property Services() As NetworkService
End Class
Class NetworkService Inherits NSNetServiceMBS
EventHandler Sub DidNotResolve(Error as Dictionary) System.DebugLog CurrentMethodName resolving = False resolveError = true resolveDone = False End EventHandler
EventHandler Sub DidResolveAddress() System.DebugLog CurrentMethodName resolveError = false resolving = false resolveDone = true MainScreen.FoundService Me End EventHandler
EventHandler Sub WillResolve() System.DebugLog CurrentMethodName resolving = true resolveError = false resolveDone = False End EventHandler
Property MainScreen As MainScreen
Property resolveDone As Boolean
Property resolveError As Boolean
Property resolving As Boolean
End Class
Class MySocket Inherits TCPSocket
EventHandler Sub Connected() System.DebugLog CurrentMethodName End EventHandler
EventHandler Sub DataAvailable() System.DebugLog CurrentMethodName Dim buf As String = Me.ReadAll buffer = buffer + buf Dim Delimiter As String = "}"+EndOfLine.UNIX Dim pos As Integer = buffer.IndexOf(Delimiter) While pos >= 0 Dim packet As String = buffer.Left(pos+1) Process packet buffer = buffer.Middle(pos+2) pos = buffer.IndexOf(Delimiter) Wend End EventHandler
EventHandler Sub Error(err As RuntimeException) System.DebugLog CurrentMethodName If err.ErrorNumber = 103 Then // failed to connect, so close and free Me.MainScreen.Socket = Nil Me.MainScreen = Nil Return End If If err.ErrorNumber = 102 Then // failed to connect, so close and free Me.MainScreen.StatusLabel.Text = "Connection Lost." Me.MainScreen.SocketLost Me.MainScreen.Socket = Nil Me.MainScreen = Nil Return End If Break End EventHandler
Sub Process(Packet as string) Dim j As New JSONItem(Packet) If j.HasKey("ServiceName") Then MainScreen.ServiceConnect j End If End Sub
Sub Send(j as JSONItem) Me.Write j.ToString Me.Write EndOfLine.UNIX // we send EndOfLine, so we can watch on other side for } + endofline as delimiter End Sub
Property MainScreen As MainScreen
Property buffer As string
End Class
End Project

See also:

The items on this page are in the following plugins: MBS MacFrameworks Plugin.


The biggest plugin in space...