0

i'm very new in iOS app development.I am going to use AFNetworking 3.0.I want to update location in background and send it to server. while send it to server,i want to condition: 1)if location not change i want to call service sendlocation1 2)if location change i want to call service sendlocation2. location change (near about 50 meters). Please help me.... i tried following code---- in ViewDidLoad:

(void)viewDidLoad {
[super viewDidLoad];

locationManager = [[CLLocationManager alloc] init];
locationManager.distanceFilter = kCLDistanceFilterNone;
locationManager.desiredAccuracy = kCLLocationAccuracyBest;
[locationManager startUpdatingLocation];

[locationManager requestAlwaysAuthorization]; 



float Lat = locationManager.location.coordinate.latitude;
float Long = locationManager.location.coordinate.longitude;
NSLog(@"Lat : %f  Long : %f",Lat,Long);}

i'm going to check,if i get response from server is Login Success then only send location to server, thats why i use if-else statement.like following:

if ([_str isEqualToString:@"Login Success"]) {

        UIAlertController *Loginalert=   [UIAlertController

                                          alertControllerWithTitle:@"Login Success"

                                          message:@"This app is going to close now"

                                          preferredStyle:UIAlertControllerStyleAlert];


        UIAlertAction* yesButton = [UIAlertAction

                                    actionWithTitle:@"Ok"

                                    style:UIAlertActionStyleDefault

                                    handler:^(UIAlertAction * action)

                                    {
                                        if () {
                                            [self sendlocation1];
                                        } else {
                                            [self sendlocation2];
                                        }

                                        [Loginalert dismissViewControllerAnimated:YES completion:nil];



                                    }];

        [Loginalert addAction: yesButton];




    } else {

        NSLog(@"Something Wrong");

and finally

(void)sendlocation1{
}

(void)sendlocation2{
}

please help me...how to check if location change or not? ...what should I write in if condition and send it to server.

Liju Thomas
  • 1,054
  • 5
  • 18
  • 25
Suraj Sukale
  • 1,778
  • 1
  • 12
  • 19

3 Answers3

1

Step-1

 (void)viewDidLoad {
[super viewDidLoad];

locationManager = [[CLLocationManager alloc] init];
//set the amount of metres travelled before location update is made
[locationManager setDistanceFilter:50];
locationManager.desiredAccuracy = kCLLocationAccuracyBest;
[locationManager startUpdatingLocation];

[locationManager requestAlwaysAuthorization]; 

// call the timer with 5 minutes cap using 5 * 60 = 300
[NSTimer scheduledTimerWithTimeInterval:300.0f target:self selector:@selector(sendlocation1) userInfo:nil repeats:YES];

step-2

Every 50 Meter change Device This Method is called :

 -(void)locationManager:(CLLocationManager *)manager
   didUpdateToLocation:(CLLocation *)newLocation
          fromLocation:(CLLocation *)oldLocation
{
    NSLog(@"%f",newLocation.coordinate.latitude);

    NSLog(@"%f",newLocation.coordinate.longitude);
    CLLocationDistance meters = [newLocation distanceFromLocation:oldLocation];
         if(meters >=50)
      {
         // call webservice for location is updated 
         [self sendlocation1];
      }else
      {
       // call normal method
       [self webservice_UpdateLocation];
       }
    }
}

-(void)webservice_UpdateLocation
{
    if ([_str isEqualToString:@"Login Success"]) {
        
        UIAlertController *Loginalert=   [UIAlertController
                                          
                                          alertControllerWithTitle:@"Login Success"
                                          
                                          message:@"This app is going to close now"
                                          
                                          preferredStyle:UIAlertControllerStyleAlert];

        
        UIAlertAction* yesButton = [UIAlertAction
                                    
                                    actionWithTitle:@"Ok"
                                    
                                    style:UIAlertActionStyleDefault
                                    
                                    handler:^(UIAlertAction * action)
                                    
                                    {
                                        
                                          
                                        
                                            [self sendlocation2];
                                       
                                        
                                        [Loginalert dismissViewControllerAnimated:YES completion:nil];
                                        
                                        
                                        
                                    }];
        
        [Loginalert addAction: yesButton];
        

        
        
    } else {
        
        NSLog(@"Something Wrong");
 }
Community
  • 1
  • 1
Anbu.Karthik
  • 82,064
  • 23
  • 174
  • 143
  • singe method is enough bro, its called every 50 meter distance – Anbu.Karthik May 19 '16 at 07:53
  • if location changes or not changes in some time(suppose in 5 min or 10 min) then also i want to call web_service....thats why i write that 2 service(sendlocation1 n sendlocation2)......how can i do that brother?...please solve my problem – Suraj Sukale May 19 '16 at 08:01
  • @SurajSukale - check the updated answer bro , I used the NSTimer concept, single webservice is enough – Anbu.Karthik May 19 '16 at 08:05
  • Hey @Anbu.Karthik.........after 5 min,if location changes then i want to call sendlocation1(because in that i'm going to send 5 parameters to server) and if location not changes want to call sendlocation2 (because in that i'm going to send 3 parameters to server)...... – Suraj Sukale May 19 '16 at 08:41
  • @Anbu.Karthik...i think...now you will get it properly .... so please try to give me solution for this.... – Suraj Sukale May 19 '16 at 08:44
  • @Anbu.Karthik....locationManager:didUpdateToLocation:fromLocation: is deprecated so you can expect it not to always be used. – Suraj Sukale May 19 '16 at 11:00
  • we can use does not a problem – Anbu.Karthik May 19 '16 at 11:04
0

You can check the displacement of the 2 (coord_A, coord_B) co ordinates using CLLocation.

 CLLocationDistance distance = [coord_A distanceFromLocation:coord_B];

The distance you will receive will be in meters. You can execute your conditions in an If-Else loop with the obtained distance.

7vikram7
  • 2,764
  • 1
  • 25
  • 43
  • @7vikram7...please can you tell me,exactly what i'll wright at (coord_A and coord_B )..please? – Suraj Sukale May 19 '16 at 06:57
  • These are CLLocation objects. One of them is the old location of user and other other is the new location. You get these objects in the "locationManager:didUpdateToLocation:fromLocation:" method. – 7vikram7 May 19 '16 at 07:11
0

Please check this answer for sending location to server for every n seconds

Sending Latitude and Longitude to Server when app is in background

Community
  • 1
  • 1
Santo
  • 1,611
  • 3
  • 17
  • 37