我想使用Xcode开发一个应用程序。当我完成应用并发布它时,如何识别我的应用的附近用户。我希望能够在附近的距离连接使用相同应用程序的用户。在哪里以及如何开始学习如何操作?我希望能够向正在使用我的应用程序的人发送消息。任何建议将不胜感激, 谢谢,
答案 0 :(得分:4)
有几种选择。如果您想确定哪些设备真正靠近,您可以使用Core Bluetooth来检测附近的设备,但这是非常有限的范围,有点超出您的需要。
相反,您可以使用Core Location跟踪用户的位置,并每隔一分钟将此信息发送到服务器,您可以在其中保留正在运行的设备列表及其位置。然后,每个设备也可以向您的服务器发出请求,以便弄清楚附近有哪些其他用户。这样做的好处是你可以处理很多东西,例如范围服务器端,因此它们不受蓝牙信号强度的限制。
如果你是从UIViewController
子类中执行此操作,它可能看起来像这样:
-(void)viewDidLoad {
locationManager = [[CLLocationManager alloc] init]; //locationManager should be an instance variable
[locationManager setDesiredAccuracy:0.1];
locationManager.delegate = self; //your class should conform to the CLLocationManagerDelegate protocol
[locationManager startUpdatingLocation];
}
-(void)locationManager:(CLLocationManager *)manager didUpdateLocations:(NSArray *)locations {
CLLocation *location = [locations objectAtIndex:0];
NSMutableURLRequest *request = [NSMutableURLRequest requestWithURL:[NSURL URLWithString:@"http://example.com/mylocationupdate"]];
[request setHTTPMethod:@"POST"];
[request setHTTPBody:[[NSString stringWithFormat:@"latlon=%f,%f&identifier=%@",
location.coordinate.latitude, location.coordinate.longitude, /*something for you to identify each app user*/]
dataUsingEncoding:NSUTF8StringEncoding]];
[NSURLConnection sendAsynchronousRequest:request queue:[NSOperationQueue mainQueue]
completionHandler:^(NSURLResponse *response, NSData *data, NSError *connectionError) {
/*handle response*/
}
[manager stopUpdatingLocation];
[manager performSelector:@selector(startUpdatingLocation) withObject:nil afterDelay:60.0];
}
另一方面,您应该处理某些网址的请求,例如@"http://example.com/nearbyusers"
,这也会采用坐标对。在服务器上,您可以使用一些基本几何查询用户位置的运行列表,以确定哪些接近给定坐标。然后,您应该将此列表作为JSON或XML或其他格式返回,并让您的应用解析它以显示关闭用户。