Gps位置服务在iOS 8中不起作用

时间:2014-10-08 11:28:28

标签: ios iphone gps

我的程序在iOS7中工作正常但不是iOS8 .. locationManager此处为CLLocationManager的对象。

在开始这个代码在iOS7中工作正常时,我无法理解为什么会发生这种情况。

- (void)getCurrentLocation{
    locationManager = [[CLLocationManager alloc] init];
    geocoder = [[CLGeocoder alloc] init];
    locationManager.delegate = self;
    locationManager.distanceFilter = kCLDistanceFilterNone;
    locationManager.desiredAccuracy = kCLLocationAccuracyBest;
    [locationManager startUpdatingLocation];
}
#pragma mark - CLLocationManagerDelegate
- (void)locationManager:(CLLocationManager *)manager didChangeAuthorizationStatus:(CLAuthorizationStatus)status{
    if (status == kCLAuthorizationStatusAuthorized) {

        NSUserDefaults *notify = [NSUserDefaults standardUserDefaults];
        [notify setObject:@"GPS" forKey:@"Notification"];
        [notify synchronize];

        appDelegate = (JobDiagnosisAppDelegate *)[[UIApplication sharedApplication] delegate];
        [[UIApplication sharedApplication] setNetworkActivityIndicatorVisible:TRUE];
        [appDelegate showIndicator];
        [notify setObject:nil forKey:@"Notification"];
        [notify synchronize];
    }
}
- (void)locationManager:(CLLocationManager *)manager didFailWithError:(NSError *)error
{
    NSLog(@"didFailWithError: %@", error);
    [appDelegate.objActivityAlertView close];
    [[UIApplication sharedApplication] setNetworkActivityIndicatorVisible:FALSE];

    appDelegate = (JobDiagnosisAppDelegate *)[[UIApplication sharedApplication] delegate];
    [[UIApplication sharedApplication] setNetworkActivityIndicatorVisible:TRUE];
    [appDelegate showIndicator];
    [self callWebserviceLocation];
}

- (void)locationManager:(CLLocationManager *)manager didUpdateLocations:(NSArray *)locations
{
    NSLog(@"Resolving the Address");
    [geocoder reverseGeocodeLocation: locationManager.location completionHandler:
     ^(NSArray *placemarks, NSError *error) {

         //Get address
         CLPlacemark *placemark = [placemarks objectAtIndex:0];

         //String to address
         NSString *locatedaddress =[placemark valueForKey:@"administrativeArea"] ;
         //Print the location in the console
         NSLog(@"Currently address is: %@",locatedaddress);

         NSUserDefaults *notifyLocation = [NSUserDefaults standardUserDefaults];
         [notifyLocation setObject:locatedaddress forKey:@"notifyLocation"];
         [notifyLocation synchronize];

         locationManager.delegate = nil;
         [self callWebserviceUnRegistered];

     }];
    [locationManager stopUpdatingLocation];

}

请帮助解决这个位置问题。我是位置服务的新手。

提前感谢!!

2 个答案:

答案 0 :(得分:0)

您需要实施   [locationManager requestWhenInUseAuthorization];,或

[locationManager requestAlwaysAuthorization];

如果您没有同时提出两个请求,iOS将忽略startUpdateLocation个请求。

此外,

在Info.plist中包含NSLocationAlwaysUsageDescriptionNSLocationWhenInUseUsageDescription密钥,具体取决于您要求的权限。这个字符串将由iOS显示给用户,这样用户就可以了解为什么我们的应用需要获得许可。

所以改变你的方法

- (void)getCurrentLocation{
    locationManager = [[CLLocationManager alloc] init];
    geocoder = [[CLGeocoder alloc] init];
    locationManager.delegate = self;
    [locationManager requestWhenInUseAuthorization];
    locationManager.distanceFilter = kCLDistanceFilterNone;
    locationManager.desiredAccuracy = kCLLocationAccuracyBest;
    [locationManager startUpdatingLocation];
}

并在info.plist中添加所需的密钥。

希望这有帮助。

答案 1 :(得分:0)

首先,不要忘记,使用IOS 8,您需要将NSLocationWhenInUseUsageDescription属性添加到info.plist文件中,以便提示警报获取用户的权限并使用[locationManager requestWhenInUseAuthorization]加{{ 1}}

[locationManager requestAlwaysAuthorization];

和你的didUpdateLocations

_locationManager = [CLLocationManager new];

if(SYSTEM_IS_OS_8_OR_LATER) {
    [_locationManager requestWhenInUseAuthorization];
    [_locationManager requestAlwaysAuthorization];

}

_locationManager.delegate = self;
_locationManager.desiredAccuracy = kCLLocationAccuracyBest;
_locationManager startUpdatingLocation];