iosobjective-cbackgroundcore-locationuibackgroundtask

Sending location coordinates to server in background from iOS


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


Solution

  • 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.