Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Finding nearby app users

Tags:

ios

bluetooth

I wanted to develop an app using Xcode. When I finish the app and release it, how can I identify nearby users of my app. I would like to be able to connect users that are using the same app, at a nearby distance. Where AND how can I start learning on how to do so? I want to be able to senda message for example to a person who is using my app. Any suggestions would be greatly appreciated, Thank You,

like image 912
Can't see me Avatar asked Feb 14 '26 01:02

Can't see me


1 Answers

There are a couple options. If you want to figure out which devices are really close, you can use Core Bluetooth to detect nearby devices, but this is very limited range and somewhat beyond what you need.

Instead, you can use Core Location to keep track of the user's location, and send this info to a server every minute or so, where you can keep a running list of devices and their location. Then, each device can also make a request to your server in order to figure out what other users are nearby. This has the advantage that you can handle lots of things like range server-side, so that they aren't limited by, say, the strength of a bluetooth signal.

If you were doing this from a UIViewController subclass it might look something like this:

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

}

On the other end, you should handle requests to some URL like @"http://example.com/nearbyusers", which would also take a coordinate pair. On the server you can query the running list of users' locations with some basic geometry to figure out which ones are close to the given coordinates. Then, you should return this list as JSON or XML or some other format, and have your app parse it to display the close users.

like image 196
Jumhyn Avatar answered Feb 15 '26 14:02

Jumhyn



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!