Suraj Sukale Suraj Sukale - 6 months ago 106
Objective-C Question

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

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.
Can I do this ? Can I send location updates to the server in background mode as well as in app running mode? how?...please i trying this 1st time ,so help me...
Thanks in advance..

Answer

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.