In offline mode how to get user\'s current location in ios, is it possible to get the location by network provider?
I give you full solution.If you want to achieve this you should use core location framework.
First step:
import CoreLocation framework in .h file
#import <CoreLocation/CoreLocation.h>
Second Step: You need to add following things in your plist file
NSLocationWhenInUseUsageDescription string Whatever you want to write write here
NSLocationAlwaysUsageDescription string Whatever you want to write write here
privacy - location usage description string Whatever you want to write write here
Kindly see the screen shot below
Third step:
Add CLLocationManagerDelegate
Fourth step:
Create an instance of the CLLocationManager class and store a strong reference
@property (nonatomic , strong) CLLocationManager *locationManager;
Why do we store CLLocationManager property as strong reference here?
Keeping a strong reference to the location manager object is required until all tasks involving that object are complete. Because most location manager tasks run asynchronously, storing your location manager in a local variable is insufficient.
Now ViewController.h file
#import <UIKit/UIKit.h>
#import <CoreLocation/CoreLocation.h>
@interface ViewController : UIViewController<CLLocationManagerDelegate>
{
}
@property (nonatomic , strong) CLLocationManager *locationManager;
- (IBAction)actionGetCurrentLocation:(id)sender;
@end
Following steps to be implemented in ViewController.m
Fifth step:
Before starting to update the location we should ask permission and instantiate the CLLocationManager.Then we should start updating the location
- (IBAction)actionGetCurrentLocation:(id)sender
{
locationManager = [[CLLocationManager alloc]init];
locationManager.delegate = self;
locationManager.desiredAccuracy = kCLLocationAccuracyBest;
if([CLLocationManager locationServicesEnabled] == NO){
NSLog(@"Your location service is not enabled, So go to Settings>Location Services");
}
else{
NSLog(@"Your location service is enabled");
}
if ([locationManager respondsToSelector:@selector(requestWhenInUseAuthorization)])
{
[locationManager requestWhenInUseAuthorization];
}
[locationManager startUpdatingLocation];
}
Sixth step:
Add CLLocationManager Delegate methods
ViewController.m
#import "ViewController.h"
@interface ViewController ()
{
CLLocation *currentLocation;
}
@end
@implementation ViewController
@synthesize locationManager;
- (void)viewDidLoad
{
[super viewDidLoad];
}
- (void)didReceiveMemoryWarning
{
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
}
#pragma mark - CLLocationManagerDelegate
- (void)locationManager:(CLLocationManager *)manager didFailWithError:(NSError *)error
{
NSLog(@"didFailWithError: %@", error); //@"Error" //@"Failed to Get Your Location" //@"OK"
UIAlertController *errorAlert = [UIAlertController alertControllerWithTitle:@"Error" message:@"Failed to Get Your Location" preferredStyle:UIAlertControllerStyleAlert];
UIAlertAction *actionOK = [UIAlertAction actionWithTitle:@"OK" style:UIAlertActionStyleDefault handler:^(UIAlertAction *action){
[self dismissViewControllerAnimated:YES completion:nil];
}];
[self presentViewController:errorAlert animated:YES completion:nil];
[errorAlert addAction:actionOK];
}
- (void)locationManager:(CLLocationManager *)manager didUpdateToLocation:(CLLocation *)newLocation fromLocation:(CLLocation *)oldLocation
{
NSLog(@"didUpdateToLocation: %@", newLocation);
currentLocation = newLocation;
if (currentLocation != nil)
{
NSLog(@"%@",[NSString stringWithFormat:@"%.8f", currentLocation.coordinate.longitude]);
NSLog(@"%@",[NSString stringWithFormat:@"%.8f", currentLocation.coordinate.latitude]);
}
}
- (void)locationManager:(CLLocationManager *)manager didUpdateLocations:(NSArray<CLLocation *> *)locations
{
NSLog(@"didUpdateLocation are : %@", locations);
currentLocation = [locations lastObject];
if (currentLocation != nil)
{
NSLog(@"The latitude value is - %@",[NSString stringWithFormat:@"%.8f", currentLocation.coordinate.longitude]);
NSLog(@"The longitude value is - %@",[NSString stringWithFormat:@"%.8f", currentLocation.coordinate.latitude]);
}
}
#pragma mark - UIButton Action
- (IBAction)actionGetCurrentLocation:(id)sender
{
locationManager = [[CLLocationManager alloc]init];
locationManager.delegate = self;
locationManager.desiredAccuracy = kCLLocationAccuracyBest;
if([CLLocationManager locationServicesEnabled] == NO){
NSLog(@"Your location service is not enabled, So go to Settings>Location Services");
}
else{
NSLog(@"Your location service is enabled");
}
if ([locationManager respondsToSelector:@selector(requestWhenInUseAuthorization)])
{
[locationManager requestWhenInUseAuthorization];
}
[locationManager startUpdatingLocation];
}