IOS后台定位

来源:互联网 发布:各行业数据查询 编辑:程序博客网 时间:2024/05/22 16:53

音乐和定位可以在后台一直运行着,前提是用户同意


直接上代码

////  ViewController.m//  DingweiTest////  Created by user on 15/9/7.//  Copyright (c) 2015年 ron. All rights reserved.//#import <CoreLocation/CoreLocation.h>#import "ViewController.h"@interface ViewController ()<UIAccelerometerDelegate , CLLocationManagerDelegate> {    CLLocationManager *locationManager;}@end@implementation ViewController- (void)viewDidLoad {    [super viewDidLoad];        locationManager = [[CLLocationManager alloc] init];//初始化定位器    [locationManager setDelegate: self];//设置代理    [locationManager setDesiredAccuracy: kCLLocationAccuracyBest];//设置精确度    [locationManager startUpdatingLocation];//开启位置更新    locationManager.pausesLocationUpdatesAutomatically = NO;    // Do any additional setup after loading the view, typically from a nib.}- (void)didReceiveMemoryWarning {    [super didReceiveMemoryWarning];    // Dispose of any resources that can be recreated.}- (void)locationManager:(CLLocationManager *)manager didUpdateToLocation:(CLLocation *)newLocation fromLocation:(CLLocation *)oldLocation {    NSLog(@"1123123123");}- (void)locationManager:(CLLocationManager *)manager didFailWithError:(NSError *)error {    NSLog(@"error");}@end

还有2个地方要设置,一个是background modes 设置location updates

另外一个info.plist添加

NSLocationAlwaysUsageDescription  -- String -- 我们需要通过您的地理位置来获取周边数据(这里文字随意)

0 0