0

I am developing an application with location and getting coordinates successfully then i am calling the webservice to store the coordinates, but when i use ios 7.1 device then didupdatelocation method is not called repeadly. Here is my code

if ([CLLocationManager locationServicesEnabled])
    {
        self.locationManager = [[CLLocationManager alloc] init];
        self.locationManager.pausesLocationUpdatesAutomatically=NO;

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

and this

- (void)locationManager:(CLLocationManager *)manager didUpdateLocations:(NSArray *)locations
{
    if (UIApplication.sharedApplication.applicationState == UIApplicationStateActive)
    {
        CLLocationCoordinate2D center;

        center.latitude = [[locations objectAtIndex:([locations count]-1)] coordinate].latitude;
        center.longitude = [[locations objectAtIndex:([locations count]-1)] coordinate].longitude;

        _point = [[MKPointAnnotation alloc] init];
        _point.coordinate = center;

        if(appDelegate.strVehicleType!=nil)
        {
            [self checkInternet];
        }

        NSLog(@"Changed");

        [_locationManager stopUpdatingLocation];
    }
    else
    {
        CLLocationCoordinate2D center;

        center.latitude = [[locations objectAtIndex:([locations     count]-1)] coordinate].latitude;
        center.longitude = [[locations objectAtIndex:([locations     count]-1)] coordinate].longitude;

        _point = [[MKPointAnnotation alloc] init];

        _point.coordinate = center;

        if(appDelegate.strVehicleType!=nil)
        {
            [self checkInternet];
        }

        NSLog(@"Location Changed in Background")
        ;
        [_locationManager stopUpdatingLocation];
    }
}

I have tried above code the location updates repetedly in ios 6 and ios 7 but not updating in 7.1, So please anyone have idea where is the issue? or is their any different method to use.

Thanx in advance.

rmaddy
  • 314,917
  • 42
  • 532
  • 579
Jiten Parmar
  • 164
  • 10
  • Check out my solution for iOS 7 and above. http://stackoverflow.com/questions/18946881/background-location-services-not-working-in-ios-7/21966662#21966662 There is a blog post and also full solution from Github. – Ricky May 05 '14 at 13:58

2 Answers2

1

If you are using simulator for test than please reset simulator. if you are using device then delete app from it.

Add #import <CoreLocation/CoreLocation.h> Framework in .h file

add delegate CLLocationManagerDelegate in .h file

In viewDidload put below code

if ([CLLocationManager locationServicesEnabled])
{
    locationManager = [[CLLocationManager alloc] init];
    locationManager.pausesLocationUpdatesAutomatically=YES;

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

====== Then

- (void)locationManager:(CLLocationManager *)manager didUpdateLocations:(NSArray *)locations
{
    if (UIApplication.sharedApplication.applicationState == UIApplicationStateActive)
    {

        NSLog(@"Active");

    }
    else
    {
        NSLog(@"Non Active");
    }
}

run app, it will give you alert 'Appname' Would like to use your current location", press "Ok"

If you are using simulator for test then please take below step:

From toolBar of Simulator Debug > Location > Apple

After taking these steps simulator change location and method will call.

Vaibhav Gautam
  • 2,074
  • 17
  • 17
Vishal
  • 159
  • 2
  • 12
  • no @Viky same result didupdatelocations is not called. – Jiten Parmar Apr 10 '14 at 12:34
  • i am using device with ios 7.1 – Jiten Parmar Apr 10 '14 at 12:36
  • I have edited my answer Please take this step if you are using simulator – Vishal Apr 10 '14 at 12:36
  • this code works like charm in ios 6 and ios 7 but not working in ios7.1, also used different methods to start location update, given the distance filter, but still same issue... – Jiten Parmar Apr 10 '14 at 12:44
  • I am testing in iOS 7.1 and its working. Please try your code i just copy your code and its working fine... – Vishal Apr 10 '14 at 12:50
  • Please try after reset your simulator. – Vishal Apr 10 '14 at 12:53
  • After reset simulator run app than app will give you alert "'Appname' Would like to use your current location" than press "Ok" than follow steps :From toolBar of Simulator Debug > Location > Apple – Vishal Apr 10 '14 at 12:57
  • can u plz post the code? because i have removed to call webservice just pointed debug on didupdatelocations but still not getting the call? – Jiten Parmar Apr 10 '14 at 13:02
  • i have used your code it only called 4 times in my iPod. – Jiten Parmar Apr 10 '14 at 13:15
  • The app is in the active state after some time it's called didfailwith error delegate method, but now not calling just stopped called didupdatelocations... – Jiten Parmar Apr 10 '14 at 13:22
  • Change code From self.locationManager.pausesLocationUpdatesAutomatically=NO; To self.locationManager.pausesLocationUpdatesAutomatically=YES; – Vishal Apr 10 '14 at 13:48
  • also done that but still same result... i think there is something lacking from my side, because at your side it's working... – Jiten Parmar Apr 10 '14 at 14:18
1

Yes, because the Background App Refresh is OFF in your Device So...

Go to General > "Background App Refresh" > Turn ON.

and General > "Background App Refresh" > your app > Turn ON.

Solve your Problem

Deepesh
  • 8,065
  • 3
  • 28
  • 45