简体   繁体   English

位置管理器在 iOS8 中不起作用

[英]Location manager is not working in iOS8

Please don't mark it as duplicate because I took help from others posted answer in stack overflow.请不要将其标记为重复,因为我从其他人在堆栈溢出中发布的答案中获取了帮助。 But still I faced some issues.但我仍然面临一些问题。 Location manager delegate not get called even in real device also.即使在真实设备中也不会调用位置管理器委托。 And also it's not asking for the permission.而且它也没有请求许可。

Below is my code.下面是我的代码。

- (IBAction)getUserLocationAction:(id)sender
{
    if([CLLocationManager locationServicesEnabled])
    {
        if(!locationManager)
            locationManager = [[CLLocationManager alloc] init];

        [locationManager setDelegate:self];
        [locationManager setDesiredAccuracy:kCLLocationAccuracyBest];

        if ([locationManager respondsToSelector:@selector(requestWhenInUseAuthorization)]) {
            [locationManager requestWhenInUseAuthorization];
        }

        [locationManager startUpdatingLocation];
        //[locationManager startMonitoringSignificantLocationChanges];
    }
    else
    {
        NSLog(@"Location service is not enabled");
    }
}


#pragma mark - Location Manager Delegate- 

- (void)locationManager:(CLLocationManager *)manager didUpdateLocations:(NSArray *)locations
{
    NSLog(@"%@", locations);
    userLocation = [locations lastObject];
    NSLog(@"User Current Locatiion\nLat: %+.6f\nLong: %+.6f", [userLocation coordinate].latitude, [userLocation coordinate].longitude);

   [locationManager stopUpdatingLocation];
   //[locationManager stopMonitoringSignificantLocationChanges];
}



- (void)locationManager:(CLLocationManager *)manager didFailWithError:(NSError *)error
{
    NSLog(@"Error: %@", [error localizedDescription]);
}

I also add the below two key in my .plist file我还在我的 .plist 文件中添加了以下两个键

<key>NSLocationWhenInUseUsageDescription</key>
<string>This app want to use your location</string>
<key>NSLocationAlwaysUsageDescription</key>
<string>This app want to use your location</string>

I have also added the frameworks我还添加了框架

在此处输入图片说明

I am not sure where I am doing wrong.我不确定我哪里做错了。 Please help me.请帮我。

UPDATE更新

Firstly I think its an issue with my code but after some research it's working as expected in iOS7.首先,我认为这是我的代码的一个问题,但经过一些研究,它在 iOS7 中按预期工作。 The code is remain same, this issue occur only in iOS8.代码保持不变,此问题仅在iOS8中出现。 Please let me know what changes needs to be done for working in iOS8 also.请让我知道在 iOS8 中工作还需要做哪些更改。

You need to do some more checking:你需要做更多的检查:

- (IBAction)getUserLocationAction:(id)sender
{
    if([CLLocationManager locationServicesEnabled]) {

        if(!self.locationManager) {
            self.locationManager = [[CLLocationManager alloc] init];
            [self.locationManager setDelegate:self];
            [self.locationManager setDesiredAccuracy:kCLLocationAccuracyBest];
        }

        CLAuthorizationStatus authStatus = [CLLocationManager authorizationStatus];
        if (authStatus == kCLAuthorizationStatusNotDetermined) {
            // Check for iOS 8 method
            if ([self.locationManager respondsToSelector:@selector(requestWhenInUseAuthorization)]) {
                [self.locationManager requestWhenInUseAuthorization];
            }
            else {
                [self.locationManager startUpdatingLocation];
            }
        }
        else if(authStatus == kCLAuthorizationStatusAuthorizedAlways || authStatus == kCLAuthorizationStatusAuthorizedWhenInUse || authStatus == kCLAuthorizationStatusAuthorized) {
            [self.locationManager startUpdatingLocation];
        }
        else if(authStatus == kCLAuthorizationStatusDenied){
            NSLog(@"User did not allow location tracking.");
            // present some dialog that you want the location.
        }
        else {
            // kCLAuthorizationStatusRestricted
            // restriction on the device do not allow location tracking.
        }
    }
    else {
        NSLog(@"Location service is not enabled");
    }
}

Also you need to handle the use callback:您还需要处理使用回调:

- (void)locationManager:(CLLocationManager *)manager didChangeAuthorizationStatus:(CLAuthorizationStatus)status {
    if (status != kCLAuthorizationStatusRestricted &&  status !=kCLAuthorizationStatusDenied) {
        [self.locationManager startUpdatingLocation];
    }
}

检查您的位置访问设置..所以转到设置-->隐私-->定位服务-->您的应用

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM