Platforms to show: All Mac Windows Linux Cross-Platform
/MacFrameworks/NSNetService/Bonjour iOS
Required plugins for this example: MBS MacBase Plugin, MBS MacFrameworks Plugin, MBS Main Plugin
You find this example project in your Plugins Download as a Xojo project file within the examples folder: /MacFrameworks/NSNetService/Bonjour iOS
This example is the version from Mon, 6th Nov 2022.
Project "Bonjour iOS.xojo_binary_project"
Class App Inherits IOSApplication
EventHandler Function Open(launchOptionsHandle as Ptr) As Boolean
// register test service
const domain = "" // default
const type = "_test._tcp."
const name = "My Test Service"
const port = 12345
testService = new NSNetServiceMBS(domain, type, name, port)
testService.publish
End EventHandler
Property testService As NSNetServiceMBS
End Class
Class iPhoneScreen Inherits iosScreen
ScreenContentItem
End Class
Class iPadScreen Inherits iosScreen
ScreenContentItem
End Class
Class MainView Inherits iosView
Control myTable Inherits iOSTable
ControlInstance myTable Inherits iOSTable
Constraint Constraint 1
Constraint Constraint 2
Constraint Constraint 3
Constraint Constraint 4
End Control
EventHandler Sub Open()
DataSource = new MyTableDataSource
myTable.DataSource = DataSource
// search for types
searchForServicesOfType "_test._tcp."
searchForServicesOfType "_smb._tcp."
searchForServicesOfType "_afpovertcp._tcp."
searchForServicesOfType "_printer._tcp."
searchForServicesOfType "_http._tcp."
searchForServicesOfType "_presence._tcp."
searchForServicesOfType "_whats-my-name._tcp."
searchForServicesOfType "_rfb._tcp."
End EventHandler
Protected Sub searchForServicesOfType(type as text)
dim m as new ServiceBrowser
m.dataSource = DataSource
m.table = myTable
// search for domains
m.searchForServicesOfType type
browsers.Append m
End Sub
Property Protected DataSource As MyTableDataSource
Property Protected browsers() As ServiceBrowser
End Class
Class ServiceBrowser Inherits NSNetServiceBrowserMBS
EventHandler Sub DidFindService(service as NSNetServiceMBS, moreComing as Boolean)
// make a new object so we have our events installed
dim m as new NetworkService(Service)
m.table = table
m.dataSource = dataSource
m.resolve
// keep Xojo object alive for later
dataSource.services.Append m
table.ReloadData
End EventHandler
EventHandler Sub DidNotSearch(error as Dictionary)
Break // error?
End EventHandler
EventHandler Sub DidRemoveService(service as NSNetServiceMBS, moreComing as Boolean)
System.DebugLog CurrentMethodName
dim myServices() as NetworkService = dataSource.Services
dim u as integer = UBound(myServices)
for i as integer = u downto 0
if myServices(i).Handle = Service.Handle then
myServices.Remove i
end if
next
table.ReloadData
End EventHandler
Property dataSource As MyTableDataSource
Property table As iOSTable
End Class
Class NetworkService Inherits NSNetServiceMBS
EventHandler Sub DidNotResolve(Error as Dictionary)
System.DebugLog CurrentMethodName
resolving = False
resolveError = true
resolveDone = False
table.ReloadData
End EventHandler
EventHandler Sub DidResolveAddress()
System.DebugLog CurrentMethodName
resolveError = false
resolving = false
resolveDone = true
table.ReloadData
End EventHandler
EventHandler Sub WillResolve()
System.DebugLog CurrentMethodName
resolving = true
resolveError = false
resolveDone = False
End EventHandler
Property dataSource As MyTableDataSource
Property resolveDone As Boolean
Property resolveError As Boolean
Property resolving As Boolean
Property table As iOSTable
End Class
Class MyTableDataSource
Function RowCount(table as iOSTable, section As Integer) As Integer
// Part of the iOSTableDataSource interface.
Return 5
End Function
Function RowData(table as iOSTable, section As Integer, row As Integer) As iOSTableCellData
// Part of the iOSTableDataSource interface.
Dim s As NetworkService = Services(section)
Dim n As iosTableCellData = table.createCell
Select Case row
Case 0
n.Text = s.name.ToText
n.DetailText = "Name"
Return n
Case 1
n.Text = s.Type.ToText
n.DetailText = "Type"
Return n
Case 2
n.Text = s.HostName.ToText
n.DetailText = "HostName"
Return n
Case 3
If s.resolving Then
n.Text = "..."
Elseif s.resolveError Then
n.Text = "failed"
Else
n.Text = s.addressIPv4.ToText
End If
n.DetailText = "Address"
Return n
Case 4
If s.resolveDone Then
n.Text = s.Port.ToText
Else
n.Text = ""
End If
n.DetailText = "Port"
Return n
end Select
End Function
Function SectionCount(table as iOSTable) As Integer
// Part of the iOSTableDataSource interface.
return UBound(Services)+1
End Function
Function SectionTitle(table as iOSTable, section As Integer) As Text
// Part of the iOSTableDataSource interface.
Dim n As Integer = section + 1
Dim t As Text
Select Case n Mod 10
Case 1
t = "st"
Case 2
t = "nd"
Else
t = "th"
End Select
Return n.ToText+t+" service"
End Function
Property Services() As NetworkService
End Class
End Project
See also:
The items on this page are in the following plugins: MBS MacFrameworks Plugin.