2014-10-08 81 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