2

I am developing an app which needs to send user location to server. I have to update user location every second and send it to server from background.

I am able to fetch the location co ordinates in background, but how can I send this to sever in every second ? Please help me.

I found this is working in an app - UBER

1 Answers1

0

It depends on what is your server expecting to be sent. But here is an example using AFNetworking 2.0:

- (void)sendLocation:(CLLocationCoordinate2D)coordinate
{
    AFHTTPRequestOperationManager *manager = [AFHTTPRequestOperationManager manager];

    NSDictionary *parameters = @{
                                 @"latitude": [NSNumber numberWithDouble:coordinate.latitude],
                                 @"longitude": [NSNumber numberWithDouble:coordinate.longitude]
                                 };

    [manager POST:@"http://example.com/yourendpoint"
       parameters:parameters
          success:^(AFHTTPRequestOperation *operation, id responseObject)
    {
        NSLog(@"JSON: %@", responseObject);
    }
          failure:^(AFHTTPRequestOperation *operation, NSError *error)
    {
        NSLog(@"Error: %@", error);
    }];
}

Sending user location every second is not a good idea. You should rather send it when it changes or in longer time intervals.

Hope it helps.

Rafał Sroka
  • 39,540
  • 23
  • 113
  • 143
  • 1
    If we will send Location to sever in longer time intervals then we can not show continue track like uber and OLA app. If you have any solution then please let me know. Thanks. – Parthpatel1105 Sep 29 '16 at 12:30