iOS8 CLLocationManager 在后台状态返回 Null

标签 ios ios8 cllocationmanager

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

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

最佳答案

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

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

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

enter image description here

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

希望对你有帮助

关于iOS8 CLLocationManager 在后台状态返回 Null,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30483700/

相关文章:

ios - 如何从终端访问 iOS 8 设备上的应用程序数据?

ios - Swift:调用中的额外参数 "progressblock"

ios - 在同一个 UIImageView 上缩放和固定

objective-c - iCloud 核心数据 IOS8 路径在任何 CloudDocs 容器之外

ios - 如何创建异步 NSOperation iOS?

ios - 在 swift 函数中使用数组中的单个 ckrecords 时遇到问题

swift - iOS 应用程序与 iBeacon 一起奇怪地工作

ios - CLLocation 管理器使用默认位置而不更新实际位置

javascript - 从 JavaScript 从 WKWebView 中的 iOS tmp/文件夹加载图像文件

ios - 向 tableView 添加行不起作用