[英]Finding nearby app users
我想用Xcode開發一個應用程序。 當我完成應用並發布它時,如何識別我的應用的附近用戶。 我希望能夠在附近的距離連接使用相同應用程序的用戶。 在哪里以及如何開始學習如何操作? 我希望能夠向正在使用我的應用程序的人發送消息。 任何建議將不勝感激,謝謝,
有幾種選擇。 如果您想確定哪些設備真的很接近,您可以使用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];
}
另一方面,您應該處理某些URL的請求,例如@"http://example.com/nearbyusers"
,這也會采用坐標對。 在服務器上,您可以使用一些基本幾何查詢用戶位置的運行列表,以確定哪些接近給定坐標。 然后,您應該將此列表作為JSON或XML或其他格式返回,並讓您的應用解析它以顯示關閉用戶。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.