【问题标题】:ios - How to find people using my app within a wifi networkios - 如何在 wifi 网络中找到使用我的应用程序的人
【发布时间】:2013-02-05 08:56:16
【问题描述】:

您好,我想找到所有在同一网络中使用我的应用的人,假设它是一个 wifi 网络,那么我想让所有人在该特定网络中使用我的应用。

他们存在于我的数据库 (mySQL) 中的个人资料数据及其位置。我可以根据位置获取用户但我的问题是根据网络找到它们。任何想法如何开始这样做?

【问题讨论】:

  • 实施 Bonjour 可能是一种选择,也可能使用 GameKit 来完成
  • 感谢您的回复,我以前从未使用过 Bonjour。您是否有任何示例代码或链接可以找到使用相同 wifi 网络的所有设备的 IP 地址? @rokjarc

标签: iphone ios xcode


【解决方案1】:

我并不是说使用 Bonjour (Zeroconf) 是实现您想要的最佳方式 - 但您当然可以使用它来实现您的目标。

Bonjour 有两种使用方式: - 发布服务 - 检测(浏览)可用服务

对于你的任务,你必须同时做这两个。

一个基本的描述可以在这里找到:Bonjour Overview and its application in iOS

如果您决定使用 Bonjour,那么您会在 Bonjour for Developers 上找到大量文档

基本上,你需要发布一个服务:Bonjour Programming > Section 18.2. Publishing a Service

发布类(通常是appDelegate)也应该是NSNetService 委托。

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];

您还应该处理委托方法(和一些appDelegate 方法):

-(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];
}

并浏览现有服务:Bonjour! (cocoanetics) (您只需要页面底部)

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 可能一开始可能会很棘手,但它肯定是一个有用的知识。

取决于您想要的用户体验。 Bonjour 的一个很好的替代品肯定是 成为GameKit (GKPeerPickerController) 用户实际上可以 选择要连接的设备(他想连接的对等设备)。 这应该可以在 Wi-Fi 或蓝牙中工作。

【讨论】:

  • 感谢您的详细解答。我会努力解决这个问题并让你知道。 @rokjarc
  • 没问题,祝你好运!如果您遇到任何问题,请告诉我。
【解决方案2】:

进行服务发现的一种方法是让您的程序在端口上侦听,然后在您想要被发现或轮询其他实体时在该端口上进行广播。 类似的东西。

 socket = listen_and_stuff()
 others = do_broadcast()
 while(run)
     if time to update
         others = do_broadcast
     if received request
         reply with info about self

但是,广播在一定程度上取决于路由器设置、子网等,因此它可能并非在所有情况下都有效。

【讨论】:

  • 老实说,我对 iOS 开发很陌生,虽然您的回答似乎很正确,但我无法理解从哪里开始以及在哪里使用此代码?你能解释一下吗?谢谢你的时间。 @dutt
  • 这是相当跨平台的,你要做的是阅读如何使用套接字。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2012-01-23
  • 2016-12-21
  • 1970-01-01
  • 2012-11-28
  • 2014-08-13
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多