IOS后台定位

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


直接上代码

//
//  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 -- 我们需要通过您的地理位置来获取周边数据(这里文字随意)

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值