2014-10-08 84 views
0

我的程序在iOS7中工作正常,但不在iOS8中。 locationManager这里是CLLocationManager的对象。全球定位系统位置服务不适用于iOS 8

在开始这段代码在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]; 

} 

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

在此先感谢!

+0

可能重复(http://stackoverflow.com/questions/24062509/ios-8-location-services-not-working) – Paulw11 2014-10-08 11:52:55

回答

0

需要实现 [locationManager requestWhenInUseAuthorization];

[locationManager requestAlwaysAuthorization];

如果你不做出以下两种请求的,iOS的会忽略startUpdateLocation请求。

此外,

包括NSLocationAlwaysUsageDescription或根据其权限你所要求的NSLocationWhenInUseUsageDescription键的Info.plist。该字符串将由iOS显示给用户,以便用户可以获得激动人心的想法为什么我们的应用程序需要权限。

所以你methood改变

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

而且ASLO在info.plist中添加所需的关键。

希望这会有所帮助。

+0

Bhumit你可以请添加此方法到我的代码..我是新的位置..请帮助 – Jason 2014-10-08 11:42:35

+0

@JEET:替换上述方法,并在您的info.Plist文件和位置更新应添加'NSLocationWhenInUseUsageDescription'键运行 – 2014-10-08 11:44:58

+0

不...即使它的不工作.. – Jason 2014-10-08 11:48:22

0

首先,不要忘了,与IOS 8,你需要NSLocationWhenInUseUsageDescription财产,以便添加到Info.plist文件提示警报,以获得用户的许可,并使用[locationManager requestWhenInUseAuthorization][locationManager requestAlwaysAuthorization];

_locationManager = [CLLocationManager new]; 

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

} 

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

和在didUpdateLocations

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

     _currentLocation = [locations objectAtIndex:0]; 
     //do your stuff with the location 

    } 
[iOS的8:定位服务不工作]的
+0

如何将NSLoationWhenInUseUsageDescription属性添加到info.plist文件中? – Jason 2014-10-09 11:38:35

+0

选择目标>>信息选项卡>>添加“NSLocationWhenInUseUsageDescription”作为字符串,并且该值是权限对话框中的子消息。(所以不是真的需要) – Smiless 2014-10-09 13:15:45