didUpdateLocation Method Never Called

后端 未结 6 895
南旧
南旧 2021-01-05 10:40

I am making one app on iphone sdk4.0.In that did update location method never called. I have given my code below.Please help.Thanks in advance.

-(id)init
{
          


        
相关标签:
6条回答
  • 2021-01-05 10:49

    After you alloc and init your location manager, check to see if location services are allowed. This code is from Apple's "LocateMe" code sample

    if (locationManager.locationServicesEnabled == NO) {
        UIAlertView *servicesDisabledAlert = [[UIAlertView alloc] initWithTitle:@"Location Services Disabled" message:@"You currently have all location services for this device disabled. If you proceed, you will be asked to confirm whether location services should be reenabled." delegate:nil cancelButtonTitle:@"OK" otherButtonTitles:nil];
        [servicesDisabledAlert show];
        [servicesDisabledAlert release];
    }
    

    If you're testing with the simulator, try testing with a device instead. The simulator requires that the computer's WiFi is turned on, and that it can locate a WiFi base station that's in it's database of known locations.

    0 讨论(0)
  • 2021-01-05 10:52

    If you are testing this on the simulator it will not work.

    This happens because that method is only called when the device changes location (and the simulator never changes location).

    0 讨论(0)
  • 2021-01-05 10:55

    Furthermore in iOS8 you must have two extra things:

    • Add a key to your Info.plist and request authorization from the location manager asking it to start.

      • NSLocationWhenInUseUsageDescription

      • NSLocationAlwaysUsageDescription

    • You need to request authorization for the corresponding location method.

      • [self.locationManager requestWhenInUseAuthorization]

      • [self.locationManager requestAlwaysAuthorization]

    Code example:

    self.locationManager = [[CLLocationManager alloc] init];
    self.locationManager.delegate = self;
    // Check for iOS 8. Without this guard the code will crash with "unknown selector" on iOS 7.
    if ([self.locationManager respondsToSelector:@selector(requestWhenInUseAuthorization)]) {
        [self.locationManager requestWhenInUseAuthorization];
    }
    [self.locationManager startUpdatingLocation];
    

    Source: http://nevan.net/2014/09/core-location-manager-changes-in-ios-8/

    0 讨论(0)
  • 2021-01-05 10:55

    Does -locationManager:didFailWithError: in your delegate ever get called? Maybe you denied access to location data at some point and now don't get prompted, but access is denied.

    0 讨论(0)
  • 2021-01-05 11:03

    Make sure you added CLLocationManager as a property.

    @property (nonatomic , strong) CLLocationManager *locationManager;

    0 讨论(0)
  • 2021-01-05 11:08

    HI, your codes seems ok. now these can be possible reasons :

    on your init: try to check if there is locationServicesEnabled or not.

    locationManager = [[CLLocationManager alloc] init];
    if(locationManager.locationServicesEnabled == NO){
        //Your location service is not enabled, Settings>Location Services  
    }
    

    other reason, you may disallow to get location to your app. solution: simply remove your application from iPhone and re-build, now it should popup dialog to Allow location.

    use this to check error

    - (void)locationManager:(CLLocationManager *)manager
           didFailWithError:(NSError *)error
    {       
        NSLog(@"Error while getting core location : %@",[error localizedFailureReason]);
        if ([error code] == kCLErrorDenied) {
            //you had denied 
        }
        [manager stopUpdatingLocation];
    }
    

    otherwise all seems ok, you were already running ios 4.0 , which can be install in iPhone 3G and later, if it was iPhone 2g, then this problem may occur.

    0 讨论(0)
提交回复
热议问题