核心位置不在ios8中工作。

时间:2022-07-10 01:26:43

I'm trying to use significant change location monitoring in iOS 8, but the didUpdateLocations method is never called. Here is the code for setting up the location manager:

我尝试在ios8中使用重要的更改位置监视,但是didUpdateLocations方法从来没有调用过。下面是设置位置管理器的代码:

- (void)viewDidLoad
{
    [super viewDidLoad];
    CLLocationManager *locationManager = [[CLLocationManager alloc] init];
    locationManager.delegate = self;
    [locationManager requestWhenInUseAuthorization];
    [locationManager startMonitoringSignificantLocationChanges];
}

Despite calling requestWhenInUseAuthorization, nothing pops up to ask the user to authorize it. I have set NSLocationWhenInUseUsageDescription, and it still does not work. didChangeAuthorizationStatus and didFailWithError are also never called. EDIT: I was able to get it to ask the user to allow location services, but even if you click allow it never shows the location.

尽管在调用requestWhenInUseAuthorization时,没有任何东西会弹出请求用户授权它。我已经设置了NSLocationWhenInUseUsageDescription,但它仍然不起作用。didChangeAuthorizationStatus和didFailWithError也没有被调用。编辑:我可以让它让用户允许位置服务,但即使你点击允许,它也不会显示位置。

8 个解决方案

#1


17  

There is currently a bug in iOS8 beta5 which always deactivate geolocation service for your app (at least for mine).

目前iOS8 beta5中有一个bug,它总是为你的应用程序(至少是我的应用程序)禁用地理定位服务。

Go in settings > Privacy > Location services > Your app > Always.

设置>隐私>定位服务>你的应用>。

But I don't know why, but even if you set it to Always this setting will auto-deactivate, so please be patient and often return in the settings to configure again your app location.

但是我不知道为什么,但是即使你设置它总是这个设置会自动停用,所以请耐心,并且经常返回设置重新配置你的应用程序位置。

#2


11  

Try declare CLLocationManager *locationManager in your header file then it works. Unless I have declare it as a global variable it does not ask for permission and update location.

尝试在头文件中声明CLLocationManager *locationManager,然后它就可以工作了。除非我声明它是一个全局变量,否则它不会请求权限和更新位置。

.h

. h

#import <UIKit/UIKit.h>
#import <CoreLocation/CoreLocation.h>

@interface ViewController : UIViewController <CLLocationManagerDelegate>
{
    CLLocationManager *locationManager;
}

.m

00

- (void)viewDidLoad {
    [super viewDidLoad];
    locationManager = [[CLLocationManager alloc] init];
    [locationManager setDelegate:(id)self];
    [locationManager requestWhenInUseAuthorization];
    [locationManager startMonitoringSignificantLocationChanges];
    [locationManager startUpdatingLocation];
}

Delegate methods

委托方法

-(void)locationManager:(CLLocationManager *)manager didUpdateLocations:(NSArray *)locations {
    NSLog(@"Getting Location");
}

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

info.plis

info.plis

核心位置不在ios8中工作。

#3


4  

into .plist file add 核心位置不在ios8中工作。

到.plist文件中添加

locationManager = [[CLLocationManager alloc] init];
            [locationManager setDelegate:self];
            [locationManager setDesiredAccuracy:kCLLocationAccuracyNearestTenMeters];

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

[locationManager startUpdatingLocation];


 - (void)locationManager:(CLLocationManager *)manager didUpdateLocations:(NSArray *)locations
    {
     currentLocation = [locations lastObject];
    }

#4


2  

All the steps look complete but run :startUpdatingLocation only after you check for :didChangeAuthorizationStatus.

所有的步骤看起来都是完整的,但是运行:只在您检查后才会启动:didChangeAuthorizationStatus。

At this point, please make sure that the authorizationStatus is not kCLAuthorizationStatusNotDetermined or kCLAuthorizationStatusDenied.

此时,请确保authorizationStatus不是kclauthorizationstatusnot或kCLAuthorizationStatusDenied。

If it is either, check for the string entries in info.plist.

如果是,则检查info.plist中的字符串条目。

If you feel all of it is correct, Make sure that you uninstall the app from the device. Perform a clean build and then run the app on the device.

如果你觉得所有的都是正确的,请确保你从设备上卸载了应用程序。执行一个干净的构建,然后在设备上运行应用程序。

#5


2  

I had a similar problem migrating my app to iOS 8. As the original code as based on sample code provided by Apple https://developer.apple.com/library/ios/samplecode/GeocoderDemo/Introduction/Intro.html I checked if it had been updated and it had. The main difference is in this function the bit relevant to iOS 8, has been commented. This worked for me.

我有一个类似的问题将我的应用迁移到iOS 8。作为最初的代码,基于苹果https://developer.apple.com/library/ios/samplecode/geocoderdemo/tion/intro.html,我检查了它是否已经更新过了。主要的区别在于这个函数与ios8相关,已经被注释了。这为我工作。

- (void)startUpdatingCurrentLocation
{
 // if location services are restricted do nothing
    if ([CLLocationManager authorizationStatus] == kCLAuthorizationStatusDenied || 
       [CLLocationManager authorizationStatus] == kCLAuthorizationStatusRestricted )
    {
       return;
    }

// if locationManager does not currently exist, create it
   if (self.locationManager == nil)
   {
        _locationManager = [[CLLocationManager alloc] init];
        [self.locationManager setDelegate:self];
        self.locationManager.distanceFilter = 10.0f; // we don't need to be any more accurate than 10m
   }

   // for iOS 8, specific user level permission is required,
   // "when-in-use" authorization grants access to the user's location
   //
  // important: be sure to include NSLocationWhenInUseUsageDescription along with its
  // explanation string in your Info.plist or startUpdatingLocation will not work.
  //
  if ([_locationManager respondsToSelector:@selector(requestWhenInUseAuthorization)])
  {
      [_locationManager requestWhenInUseAuthorization];
  }

  [_locationManager startUpdatingLocation];

  [self showCurrentLocationSpinner:YES];
}

#6


1  

Try

试一试

  1. Background mode on in capabilities

    后台模式的功能。

  2. #import <CoreLocation/CoreLocation.h>

    #进口< CoreLocation / CoreLocation.h >

  3. <CLLocationManagerDelegate>

    < CLLocationManagerDelegate >

  4. locationManager = [[CLLocationManager alloc] init]; [locationManager setDelegate:(id)self]; [locationManager requestWhenInUseAuthorization];

    locationManager = [CLLocationManager alloc];[locationManager setDelegate:自我(id)];[locationManager requestWhenInUseAuthorization];

    Thats in the beginning of app

    这是应用程序的开始。

  5. NSLocationWhenInUseUsageDescription in plist

    在plist NSLocationWhenInUseUsageDescription

  6. Try to CMD + SHIFT + K

    尝试CMD + SHIFT + K。

  7. Try to turn off iPhone and then turn on - he had some cash in iOS 8 - so after 5 times - app don't ask permission to Core Location - that helped me

    试着关掉iPhone,然后打开——他在iOS 8中有一些现金——所以在5次之后,应用程序不请求允许到核心位置——这帮助了我。

#7


1  

The probable reason, why the authorization dialog is not shown with the OPs code, is that the location manager has to hang around for it to work.

可能的原因是,为什么授权对话框没有显示在OPs代码中,是因为位置管理器必须在其周围挂起才能工作。

Use a property or a static variable to keep locationManager in memory.

使用属性或静态变量将locationManager保存在内存中。

#8


0  

Maybe its because of the WhenInUseAuthorization is not enough for the "Significant Location Change" updates.

可能是因为当“重要的位置更改”更新时,授权是不够的。

When i use AlwaysAuthorization, it just works.

当我使用always授权时,它就是有效的。

i cannot find it in the docs. But you can check http://nevan.net/2014/09/core-location-manager-changes-in-ios-8/. Under "Authorization Types" heading, it states that significant location updates need AlwaysAuthorization.

我在文档里找不到。但是您可以查看http://nevan.net/2014/09/09/core-locationmanagerchanges-in -ios-8/。在“授权类型”标题下,它声明重要的位置更新需要经常授权。

#1


17  

There is currently a bug in iOS8 beta5 which always deactivate geolocation service for your app (at least for mine).

目前iOS8 beta5中有一个bug,它总是为你的应用程序(至少是我的应用程序)禁用地理定位服务。

Go in settings > Privacy > Location services > Your app > Always.

设置>隐私>定位服务>你的应用>。

But I don't know why, but even if you set it to Always this setting will auto-deactivate, so please be patient and often return in the settings to configure again your app location.

但是我不知道为什么,但是即使你设置它总是这个设置会自动停用,所以请耐心,并且经常返回设置重新配置你的应用程序位置。

#2


11  

Try declare CLLocationManager *locationManager in your header file then it works. Unless I have declare it as a global variable it does not ask for permission and update location.

尝试在头文件中声明CLLocationManager *locationManager,然后它就可以工作了。除非我声明它是一个全局变量,否则它不会请求权限和更新位置。

.h

. h

#import <UIKit/UIKit.h>
#import <CoreLocation/CoreLocation.h>

@interface ViewController : UIViewController <CLLocationManagerDelegate>
{
    CLLocationManager *locationManager;
}

.m

00

- (void)viewDidLoad {
    [super viewDidLoad];
    locationManager = [[CLLocationManager alloc] init];
    [locationManager setDelegate:(id)self];
    [locationManager requestWhenInUseAuthorization];
    [locationManager startMonitoringSignificantLocationChanges];
    [locationManager startUpdatingLocation];
}

Delegate methods

委托方法

-(void)locationManager:(CLLocationManager *)manager didUpdateLocations:(NSArray *)locations {
    NSLog(@"Getting Location");
}

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

info.plis

info.plis

核心位置不在ios8中工作。

#3


4  

into .plist file add 核心位置不在ios8中工作。

到.plist文件中添加

locationManager = [[CLLocationManager alloc] init];
            [locationManager setDelegate:self];
            [locationManager setDesiredAccuracy:kCLLocationAccuracyNearestTenMeters];

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

[locationManager startUpdatingLocation];


 - (void)locationManager:(CLLocationManager *)manager didUpdateLocations:(NSArray *)locations
    {
     currentLocation = [locations lastObject];
    }

#4


2  

All the steps look complete but run :startUpdatingLocation only after you check for :didChangeAuthorizationStatus.

所有的步骤看起来都是完整的,但是运行:只在您检查后才会启动:didChangeAuthorizationStatus。

At this point, please make sure that the authorizationStatus is not kCLAuthorizationStatusNotDetermined or kCLAuthorizationStatusDenied.

此时,请确保authorizationStatus不是kclauthorizationstatusnot或kCLAuthorizationStatusDenied。

If it is either, check for the string entries in info.plist.

如果是,则检查info.plist中的字符串条目。

If you feel all of it is correct, Make sure that you uninstall the app from the device. Perform a clean build and then run the app on the device.

如果你觉得所有的都是正确的,请确保你从设备上卸载了应用程序。执行一个干净的构建,然后在设备上运行应用程序。

#5


2  

I had a similar problem migrating my app to iOS 8. As the original code as based on sample code provided by Apple https://developer.apple.com/library/ios/samplecode/GeocoderDemo/Introduction/Intro.html I checked if it had been updated and it had. The main difference is in this function the bit relevant to iOS 8, has been commented. This worked for me.

我有一个类似的问题将我的应用迁移到iOS 8。作为最初的代码,基于苹果https://developer.apple.com/library/ios/samplecode/geocoderdemo/tion/intro.html,我检查了它是否已经更新过了。主要的区别在于这个函数与ios8相关,已经被注释了。这为我工作。

- (void)startUpdatingCurrentLocation
{
 // if location services are restricted do nothing
    if ([CLLocationManager authorizationStatus] == kCLAuthorizationStatusDenied || 
       [CLLocationManager authorizationStatus] == kCLAuthorizationStatusRestricted )
    {
       return;
    }

// if locationManager does not currently exist, create it
   if (self.locationManager == nil)
   {
        _locationManager = [[CLLocationManager alloc] init];
        [self.locationManager setDelegate:self];
        self.locationManager.distanceFilter = 10.0f; // we don't need to be any more accurate than 10m
   }

   // for iOS 8, specific user level permission is required,
   // "when-in-use" authorization grants access to the user's location
   //
  // important: be sure to include NSLocationWhenInUseUsageDescription along with its
  // explanation string in your Info.plist or startUpdatingLocation will not work.
  //
  if ([_locationManager respondsToSelector:@selector(requestWhenInUseAuthorization)])
  {
      [_locationManager requestWhenInUseAuthorization];
  }

  [_locationManager startUpdatingLocation];

  [self showCurrentLocationSpinner:YES];
}

#6


1  

Try

试一试

  1. Background mode on in capabilities

    后台模式的功能。

  2. #import <CoreLocation/CoreLocation.h>

    #进口< CoreLocation / CoreLocation.h >

  3. <CLLocationManagerDelegate>

    < CLLocationManagerDelegate >

  4. locationManager = [[CLLocationManager alloc] init]; [locationManager setDelegate:(id)self]; [locationManager requestWhenInUseAuthorization];

    locationManager = [CLLocationManager alloc];[locationManager setDelegate:自我(id)];[locationManager requestWhenInUseAuthorization];

    Thats in the beginning of app

    这是应用程序的开始。

  5. NSLocationWhenInUseUsageDescription in plist

    在plist NSLocationWhenInUseUsageDescription

  6. Try to CMD + SHIFT + K

    尝试CMD + SHIFT + K。

  7. Try to turn off iPhone and then turn on - he had some cash in iOS 8 - so after 5 times - app don't ask permission to Core Location - that helped me

    试着关掉iPhone,然后打开——他在iOS 8中有一些现金——所以在5次之后,应用程序不请求允许到核心位置——这帮助了我。

#7


1  

The probable reason, why the authorization dialog is not shown with the OPs code, is that the location manager has to hang around for it to work.

可能的原因是,为什么授权对话框没有显示在OPs代码中,是因为位置管理器必须在其周围挂起才能工作。

Use a property or a static variable to keep locationManager in memory.

使用属性或静态变量将locationManager保存在内存中。

#8


0  

Maybe its because of the WhenInUseAuthorization is not enough for the "Significant Location Change" updates.

可能是因为当“重要的位置更改”更新时,授权是不够的。

When i use AlwaysAuthorization, it just works.

当我使用always授权时,它就是有效的。

i cannot find it in the docs. But you can check http://nevan.net/2014/09/core-location-manager-changes-in-ios-8/. Under "Authorization Types" heading, it states that significant location updates need AlwaysAuthorization.

我在文档里找不到。但是您可以查看http://nevan.net/2014/09/09/core-locationmanagerchanges-in -ios-8/。在“授权类型”标题下,它声明重要的位置更新需要经常授权。