Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

iOS8 CLLocationManager returns Null in Background State

in iOS8 when i m putting my app in back ground the CLLocationManager returns Null in location ,i have also unable Location updates in Background Modes and also do requestAlwaysAuthorization

    CLLocationManager *lm = [[CLLocationManager alloc] init];
    lm.delegate = self;
    lm.desiredAccuracy = kCLLocationAccuracyBest;
    lm.distanceFilter = kCLDistanceFilterNone;
    [lm requestAlwaysAuthorization];
    [lm startUpdatingLocation];
like image 293
Jaydeep Chauhan Avatar asked Aug 12 '26 16:08

Jaydeep Chauhan


2 Answers

Please find below code for getting location on background mode -

Please enter text for NSLocationAlwaysUsageDescription in plist file .

Please select location updates on background mode from project settings -

enter image description here

AppDelegate.h

//
//  AppDelegate.h
//  LocationTest
//
//  Created by Santu C on 28/05/15.
//  Copyright (c) 2015 company. All rights reserved.
//

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

@interface AppDelegate : UIResponder <UIApplicationDelegate>

@property (strong, nonatomic) UIWindow *window;


@property(strong,nonatomic) CLLocationManager *locationManager;


@end

AppDelegate.m

//
//  AppDelegate.m
//  LocationTest
//
//  Created by Santu C on 28/05/15.
//  Copyright (c) 2015 company. All rights reserved.
//

#import "AppDelegate.h"

@implementation AppDelegate
@synthesize locationManager;

#define CONST_TIME_INTERVAL 60.0

- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{

    [self location];



    // Override point for customization after application launch.
    return YES;
}



-(void)location
{


    NSLog(@"location called");

    self.locationManager = [[CLLocationManager alloc] init];
    self.locationManager.delegate = self;
    self.locationManager.distanceFilter = kCLDistanceFilterNone;
    self.locationManager.desiredAccuracy = kCLLocationAccuracyBestForNavigation;
    self.locationManager.pausesLocationUpdatesAutomatically = NO;

#if __IPHONE_OS_VERSION_MAX_ALLOWED >= 80000

    if ([self.locationManager respondsToSelector:@selector(requestAlwaysAuthorization)])
    {
        [self.locationManager requestAlwaysAuthorization];
    }
#endif

    [self.locationManager startUpdatingLocation];

}


- (void)startUpdatingLocation
{
    NSLog(@"startUpdatingLocation called");

    self.locationManager.delegate = self;
    [self.locationManager startUpdatingLocation];
}

- (void)locationManager:(CLLocationManager *)manager didUpdateLocations:(NSArray *)locations
{


    NSLog(@"locationManager didUpdateLocations");

    CLLocation *newLocation = (CLLocation *)[locations lastObject];


    NSLog(@"dLongitude : %f", newLocation.coordinate.latitude);
    NSLog(@"dLatitude : %f", newLocation.coordinate.longitude);



}




- (void)applicationDidEnterBackground:(UIApplication *)application
{
    // Use this method to release shared resources, save user data, invalidate timers, and store enough application state information to restore your application to its current state in case it is terminated later. 
    // If your application supports background execution, this method is called instead of applicationWillTerminate: when the user quits.

     [self startUpdatingLocation];
}



@end

Hope this will help you

like image 184
Santu C Avatar answered Aug 15 '26 05:08

Santu C


do put code in viewDidLoad about CLLocationManger

CLLocationManager *lm = [[CLLocationManager alloc] init];
lm.delegate = self;
lm.desiredAccuracy = kCLLocationAccuracyBest;
lm.distanceFilter = kCLDistanceFilterNone;
[lm requestAlwaysAuthorization];
[lm startUpdatingLocation];

Problem solved..

like image 37
Jaydeep Chauhan Avatar answered Aug 15 '26 07:08

Jaydeep Chauhan