sending current location to server in background as well as in running app using AFNetworking 3
Asked Answered
S

1

1

I tried to search lot for this, but i didn't get exact answer what i want.

my question is

Can I send user's current location to server when app is running as well as in background.

For example: In my app i want to store the user's current location in every 2min and then send his current location to server.as well as if user move 50 meters within less than 2min then also. How can i do this ? Can i send location updates to the server in background mode as well as foreground mode? if yes then how?

I tried like following way:

 self.locations = [[NSMutableArray alloc] init];
self.locationManager = [[CLLocationManager alloc] init];
self.locationManager.distanceFilter = 10;

self.locationManager.desiredAccuracy = kCLLocationAccuracyBest;
self.locationManager.delegate = self;
[self.locationManager requestAlwaysAuthorization];

[self.locationManager startUpdatingLocation];

...

- (void)locationManager:(CLLocationManager *)manager
didUpdateToLocation:(CLLocation *)newLocation
       fromLocation:(CLLocation *)oldLocation
{



 // Add another annotation to the map.
MKPointAnnotation *annotation = [[MKPointAnnotation alloc] init];
annotation.coordinate = newLocation.coordinate;
[self.map addAnnotation:annotation];

// Also add to our map so we can remove old values later
[self.locations addObject:annotation];

// Remove values if the array is too big
while (self.locations.count > 1)
{
    annotation = [self.locations objectAtIndex:0];
    [self.locations removeObjectAtIndex:0];

    // Also remove from the map
    [self.map removeAnnotation:annotation];
}


if (UIApplication.sharedApplication.applicationState == UIApplicationStateActive)
{
    // determine the region the points span so we can update our map's zoom.
    double maxLat = -91;
    double minLat =  91;
    double maxLon = -181;
    double minLon =  181;

    for (MKPointAnnotation *annotation in self.locations)
    {
        CLLocationCoordinate2D coordinate = annotation.coordinate;

        if (coordinate.latitude > maxLat)
            maxLat = coordinate.latitude;
        if (coordinate.latitude < minLat)
            minLat = coordinate.latitude;

        if (coordinate.longitude > maxLon)
            maxLon = coordinate.longitude;
        if (coordinate.longitude < minLon)
            minLon = coordinate.longitude;
    }

    MKCoordinateRegion region;
    region.span.latitudeDelta  = (maxLat +  90) - (minLat +  90);
    region.span.longitudeDelta = (maxLon + 180) - (minLon + 180);

    _latitude1=[NSString stringWithFormat:@"Latitude:%f",newLocation.coordinate.latitude];
    _longitude1=[NSString stringWithFormat:@"Longitude:%f",newLocation.coordinate.longitude];


    AppDelegate *appDelegate = (AppDelegate *)[[UIApplication sharedApplication] delegate];
    appDelegate.fetchtokenString1 = [NSString stringWithString:_latitude1];
    appDelegate.fetchedriveridString1 = [NSString stringWithString:_longitude1];





    NSLog(@"%@",_latitude1);
    NSLog(@"%@",_longitude1);


    // the center point is the average of the max and mins
    region.center.latitude  = minLat + region.span.latitudeDelta / 2;
    region.center.longitude = minLon + region.span.longitudeDelta / 2;

    // Set the region of the map.
    [self.map setRegion:region animated:YES];
}
else
{
    NSLog(@"App is backgrounded. New location is %@", newLocation);
}

Thanks in advance..

Speight answered 20/5, 2016 at 5:20 Comment(5)
can you post your code..Adenocarcinoma
your problem is not resolved on yesterdayAppall
This is another issue i'm [email protected] want to send Current location in FOREGROUND as well as BACKGROUND in every 2MIN as well as (if user move 50meters within 2 min. then also)......your yesterday's answer really best...thanks for that...but this in new issue i'm facing...please help meSpeight
@Anbu.Karthik....please can we chat regarding to this issue?Speight
may i know why question is down voted??Speight
A
0

Check this when your app is running in foreground.

How can I get current location from user in iOS

Check this to send location when your app is in background.

Sending Latitude and Longitude to Server when app is in background

after getting latitude and longitude, send by using AFNetworking framework.

OR

If you need from scratch if you dont know how to get location then follow the below steps to get latitude and longitude.

Add this below two string types in your info.pList

enter image description here

Then add CoreLocation Framework in your project.

Import it to your ViewController.h

 #import <UIKit/UIKit.h>
 #import <CoreLocation/CoreLocation.h>

 @interface ViewController : UIViewController<CLLocationManagerDelegate> 
 @property(nonatomic, retain) CLLocationManager *locationManager;

 @end

Then do below one in your ViewController.m

 #define IS_OS_8_OR_LATER ([[[UIDevice currentDevice] systemVersion] floatValue] >= 8.0)

 #import "ViewController.h"

 @interface ViewController ()

 @end

 @implementation ViewController

 - (void)viewDidLoad {
[super viewDidLoad];
// Do any additional setup after loading the view, typically from a nib.

self.locationManager = [[CLLocationManager alloc] init];
self.locationManager.delegate = self;
#ifdef __IPHONE_8_0
if(IS_OS_8_OR_LATER) {
    // Use one or the other, not both. Depending on what you put in info.plist
    [self.locationManager requestWhenInUseAuthorization];
    [self.locationManager requestAlwaysAuthorization];
}
#endif
[self.locationManager startUpdatingLocation];


}

-(void)viewWillAppear:(BOOL)animated {

NSLog(@"latitude: %f, longitude: %f",self.locationManager.location.coordinate.latitude,self.locationManager.location.coordinate.longitude);
}
- (void)didReceiveMemoryWarning {
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
}

Log will show the latitude and longitude.

Adenocarcinoma answered 20/5, 2016 at 5:26 Comment(7)
I get current location in foreground as well as background bro..... but read my question properly.. i want to send location when location changes....as well as if location not changes since last 2 min. then also send location to server.....@AdenocarcinomaSpeight
@SurajSukale you said you didnt even get location properly so i posted above one to get lat longAdenocarcinoma
i want to fetch user's current location in every 2min and then send his current location to server.as well as if user move 50 meters within less than 2min then also. Can I do this ? ..this is my actual problem.... please help me to solve this issue brother....@AdenocarcinomaSpeight
I posted one link use it, and also take lat long for every two mins and compare them, if user changes then update or else dont update. take a NStimer to check it. use CLLocation coordinate distance method. it will help you. put time interval 2 minutes and update the locationAdenocarcinoma
This will give you the distance, you can check with this whether user moved or not. CLLocation *location2 = [[CLLocation alloc] initWithLatitude:latitude1 longitude:longitude1]; CLLocation *location = [[CLLocation alloc] initWithLatitude:latitude longitude:longitude]; CLLocationDistance distance = [location distanceFromLocation:location2]; distanceInKm = distance/1000.0;Adenocarcinoma
@SurajSukale worked? u can upvote my comment if it helpedAdenocarcinoma
Let us continue this discussion in chat.Speight

© 2022 - 2024 — McMap. All rights reserved.