【问题标题】:iOS8 CLLocationManager returns Null in Background StateiOS8 CLLocationManager 在后台状态返回 Null
【发布时间】:2015-05-27 13:18:46
【问题描述】:

在 iOS8 中,当我将我的应用程序置于后台时,CLLocationManager 在位置返回 Null,我也无法在后台模式下进行位置更新,并且还执行 requestAlwaysAuthorization

    CLLocationManager *lm = [[CLLocationManager alloc] init];
    lm.delegate = self;
    lm.desiredAccuracy = kCLLocationAccuracyBest;
    lm.distanceFilter = kCLDistanceFilterNone;
    [lm requestAlwaysAuthorization];
    [lm startUpdatingLocation];

【问题讨论】:

  • 您能否确认您是否在 plist 中放置了 NSLocationAlwaysUsageDescription 并使用委托 - - (void)locationManager:(CLLocationManager *)manager didUpdateLocations:(NSArray *)locations
  • 是的,我已经实现了这两件事..它在我的 iOS7 设备中运行良好,但在 iOS8 中却不行
  • 我已经实现了相同的东西,它在后台模式下对我来说工作正常。请检查几件事,您对 CLLocationManger 有很强的参考。在标题中创建属性。
  • @Santu C ...在将其设置为标题后。 CLLocation *CurrentLocation 有时会为空。
  • 请以这种方式尝试@property (strong, nonatomic) CLLocationManager *locationManager;

标签: ios ios8 cllocationmanager


【解决方案1】:

请在下面找到在后台模式下获取位置的代码 -

请在 plist 文件中输入 NSLocationAlwaysUsageDescription 的文本。

请从项目设置中选择后台模式的位置更新 -

AppDelegate.h

//
//  AppDelegate.h
//  LocationTest
//
//  Created by Santu C on 28/05/15.
//  Copyright (c) 2015 company. All rights reserved.
//

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

@interface AppDelegate : UIResponder <UIApplicationDelegate>

@property (strong, nonatomic) UIWindow *window;


@property(strong,nonatomic) CLLocationManager *locationManager;


@end

AppDelegate.m

//
//  AppDelegate.m
//  LocationTest
//
//  Created by Santu C on 28/05/15.
//  Copyright (c) 2015 company. All rights reserved.
//

#import "AppDelegate.h"

@implementation AppDelegate
@synthesize locationManager;

#define CONST_TIME_INTERVAL 60.0

- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{

    [self location];



    // Override point for customization after application launch.
    return YES;
}



-(void)location
{


    NSLog(@"location called");

    self.locationManager = [[CLLocationManager alloc] init];
    self.locationManager.delegate = self;
    self.locationManager.distanceFilter = kCLDistanceFilterNone;
    self.locationManager.desiredAccuracy = kCLLocationAccuracyBestForNavigation;
    self.locationManager.pausesLocationUpdatesAutomatically = NO;

#if __IPHONE_OS_VERSION_MAX_ALLOWED >= 80000

    if ([self.locationManager respondsToSelector:@selector(requestAlwaysAuthorization)])
    {
        [self.locationManager requestAlwaysAuthorization];
    }
#endif

    [self.locationManager startUpdatingLocation];

}


- (void)startUpdatingLocation
{
    NSLog(@"startUpdatingLocation called");

    self.locationManager.delegate = self;
    [self.locationManager startUpdatingLocation];
}

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


    NSLog(@"locationManager didUpdateLocations");

    CLLocation *newLocation = (CLLocation *)[locations lastObject];


    NSLog(@"dLongitude : %f", newLocation.coordinate.latitude);
    NSLog(@"dLatitude : %f", newLocation.coordinate.longitude);



}




- (void)applicationDidEnterBackground:(UIApplication *)application
{
    // Use this method to release shared resources, save user data, invalidate timers, and store enough application state information to restore your application to its current state in case it is terminated later. 
    // If your application supports background execution, this method is called instead of applicationWillTerminate: when the user quits.

     [self startUpdatingLocation];
}



@end

希望对你有帮助

【讨论】:

    【解决方案2】:

    在 viewDidLoad 中添加关于 CLLocationManger 的代码

    CLLocationManager *lm = [[CLLocationManager alloc] init];
    lm.delegate = self;
    lm.desiredAccuracy = kCLLocationAccuracyBest;
    lm.distanceFilter = kCLDistanceFilterNone;
    [lm requestAlwaysAuthorization];
    [lm startUpdatingLocation];
    

    问题解决了..

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2017-10-25
      • 1970-01-01
      相关资源
      最近更新 更多