![](/img/trans.png)
[英]Connecting a viewcontroller to a delegate class - CLLocationManager / iOS8
[英]iOS8 CLLocationManager returns Null in Background State
在iOS8中,當我將我的應用程序放在后台時,CLLocationManager在位置返回Null,我也無法在后台模式中進行位置更新,並且還執行requestAlwaysAuthorization
CLLocationManager *lm = [[CLLocationManager alloc] init];
lm.delegate = self;
lm.desiredAccuracy = kCLLocationAccuracyBest;
lm.distanceFilter = kCLDistanceFilterNone;
[lm requestAlwaysAuthorization];
[lm startUpdatingLocation];
請在下面找到有關在背景模式下獲取位置的代碼 -
請在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
希望對你有幫助
將代碼放在viewDidLoad中關於CLLocationManger
CLLocationManager *lm = [[CLLocationManager alloc] init];
lm.delegate = self;
lm.desiredAccuracy = kCLLocationAccuracyBest;
lm.distanceFilter = kCLDistanceFilterNone;
[lm requestAlwaysAuthorization];
[lm startUpdatingLocation];
問題解決了..
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.