Hi I want to find all people using my app within a same network, say if its a wifi network then i want to get all people using my app in that particular network.
their profile data existing in my database (mySQL) with their location. I can get the users As per location But my Question is to find them as per network. Any ideas how to start to do that?
Im not saying using Bonjour (Zeroconf) is the best way to achieve what you want - but you can certainly achieve your goal using it.
Bonjour is used in two ways: - publishing a service - detecting (browsing for) available services
For your task you'd have to do both.
One basic description can be found here: Bonjour Overview and its application in iOS
If you decide to use Bonjour then you'll find a lot of documentation on Bonjour for Developers
Basically, you need to publish a service: Bonjour Programming > Section 18.2. Publishing a Service
The publishing class (usually appDelegate
) should be also a NSNetService
delegate.
NSNetService *netService; //an ivar or a property
//creating and publishing a service
netService = [[NSNetService alloc] initWithDomain:@""
type:@"_yourservicename._tcp."
name:@""
port:9876];
//if your app actually acts as a server port: should be it's port,
//otherwise it could be any free port
netService.delegate = self;
[netService publish];
You should also handle delegate methods (and some appDelegate
methods):
-(void)netService:(NSNetService *)aNetService
didNotPublish:(NSDictionary *)dict
{
NSLog(@"Service did not publish: %@", dict);
}
- (void)applicationWillTerminate:(UIApplication *)application {
//---stop the service when the application is terminated---
[netService stop];
}
- (void)applicationDidEnterBackground:(UIApplication *)application {
//---stop the service when the application is paused---
[netService stop];
}
- (void)applicationWillEnterForeground:(UIApplication *)application
{
[netService publish];
}
And browse for existing services: Bonjour! (cocoanetics) (you'll only need the bottom part of the page)
serviceBrowser = [[NSNetServiceBrowser alloc] init];
serviceBrowser.delegate = self;
[serviceBrowser searchForServicesOfType:@"_yourservicename._tcp." inDomain:@""];
- (void)netServiceBrowser:(NSNetServiceBrowser *)aNetServiceBrowser
didFindService:(NSNetService *)aNetService moreComing:(BOOL)moreComing
{
[self willChangeValueForKey:@"foundServices"];
[_foundServices addObject:aNetService];
[self didChangeValueForKey:@"foundServices"];
NSLog(@"found: %@", aNetService);
}
- (void)netServiceBrowser:(NSNetServiceBrowser *)aNetServiceBrowser
didRemoveService:(NSNetService *)aNetService moreComing:(BOOL)moreComing
{
[self willChangeValueForKey:@"foundServices"];
[_foundServices removeObject:aNetService];
[self didChangeValueForKey:@"foundServices"];
NSLog(@"removed: %@", aNetService);
}
PS: Bonjour might be tricky to start with but it is certainly a useful knowledge.
Depends on what user experience exactly you want. A good alternative to Bonjour would most certainly be GameKit
( GKPeerPickerController ) where user can actually select to which devices (peers he wants to connect to). This should work in Wi-Fi or via BlueTooth.
One way of doing service discovery is to have your program listen on a port and then doing a broadcast on that port when you want to be discovered or poll for other entities. Something along these lines.
socket = listen_and_stuff()
others = do_broadcast()
while(run)
if time to update
others = do_broadcast
if received request
reply with info about self
But, broadcast depends a bit on router settings, subnetworks and so on so it might not work in all cases.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.