为了账号安全,请及时绑定邮箱和手机立即绑定

iOS后台持续定位实现方法

标签:
iOS

1.选中target-->Gapability,打开Background Modes模式,并勾选Location updates

https://img1.sycdn.imooc.com//5d5e80ec0001746207080318.png

设置

2.代码如下:

ViewController.h
<pre>#import <UIKit/UIKit.h>

import <CoreLocation/CoreLocation.h>

@interface ViewController : UIViewController<CLLocationManagerDelegate>

//定位管理对象
@property(nonatomic,strong)CLLocationManager *manager;

@end</pre>
ViewController.m
<pre>#import "ViewController.h"

@interface ViewController ()
{
CLGeocoder *_coder;
//存储上一次的位置
}
@end

@implementation ViewController

  • (void)viewDidLoad
    {
    [super viewDidLoad];
    self.view.backgroundColor=[UIColor whiteColor];
    //1.创建定位管理对象
    _manager=[[CLLocationManager alloc]init];
    _coder=[[CLGeocoder alloc]init];
    //2.设置属性 distanceFilter、desiredAccuracy
    _manager.distanceFilter=kCLDistanceFilterNone;//实时更新定位位置
    _manager.desiredAccuracy=kCLLocationAccuracyBest;//定位精确度

    if([_manager respondsToSelector:@selector(requestAlwaysAuthorization)])
    {
    [_manager requestAlwaysAuthorization];
    }
    //该模式是抵抗程序在后台被杀,申明不能够被暂停
    _manager.pausesLocationUpdatesAutomatically=NO;
    //3.设置代理
    _manager.delegate=self;
    //4.开始定位
    [_manager startUpdatingLocation];
    //5.获取朝向
    [_manager startUpdatingHeading];

}

pragma mark-CLLocationManager代理方法

//定位失败时调用的方法
-(void)locationManager:(CLLocationManager *)manager didFailWithError:(NSError *)error
{
NSLog(@"%@",error);
}
//定位成功调用的的方法
-(void)locationManager:(CLLocationManager *)manager didUpdateLocations:(NSArray *)locations
{
if(locations.count>0)
{
//        获取位置信息
CLLocation *loc=[locations lastObject];
//        获取经纬度的结构体
CLLocationCoordinate2D coor=loc.coordinate;

    CLLocation *location=[[CLLocation alloc]initWithLatitude:coor.latitude longitude:coor.longitude];
    
    [_coder reverseGeocodeLocation:location completionHandler:^(NSArray *placemarks, NSError *error) {        CLPlacemark *pmark=[placemarks firstObject];        NSLog(@"%@",pmark.addressDictionary);        NSString *city=pmark.addressDictionary[@"City"];        if([city hasSuffix:@"市辖区"])
            city=[city substringToIndex:city.length-3];        if([city hasSuffix:@"市"])
            city=[city substringToIndex:city.length-1];        NSLog(@"%@",city);
    }];
}

}
@end</pre>
Appdelegate.m
<pre>#import "AppDelegate.h"

import <CoreLocation/CoreLocation.h>

import "ViewController.h"

@interface AppDelegate ()

@end

@implementation AppDelegate

  • (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
    self.window.rootViewController=[ViewController new];
    return YES;
    }

  • (void)applicationDidEnterBackground:(UIApplication )application {
    if([CLLocationManager significantLocationChangeMonitoringAvailable])
    {
    ViewController
    vc=(ViewController *)self.window.rootViewController;
    [vc.manager stopUpdatingLocation];
    [vc.manager startMonitoringSignificantLocationChanges];
    }
    else
    {
    NSLog(@"significant Location Change not Available");
    }
    }

  • (void)applicationDidBecomeActive:(UIApplication )application {
    if([CLLocationManager significantLocationChangeMonitoringAvailable])
    {
    ViewController
    vc=(ViewController *)self.window.rootViewController;
    [vc.manager stopMonitoringSignificantLocationChanges];
    [vc.manager startUpdatingLocation];
    }
    else
    {
    NSLog(@"significant Location Change not Available");
    }
    }

@end</pre>



作者:CGPointZero
链接:https://www.jianshu.com/p/16f3eef4d4ff


点击查看更多内容
TA 点赞

若觉得本文不错,就分享一下吧!

评论

作者其他优质文章

正在加载中
  • 推荐
  • 评论
  • 收藏
  • 共同学习,写下你的评论
感谢您的支持,我会继续努力的~
扫码打赏,你说多少就多少
赞赏金额会直接到老师账户
支付方式
打开微信扫一扫,即可进行扫码打赏哦
今天注册有机会得

100积分直接送

付费专栏免费学

大额优惠券免费领

立即参与 放弃机会
意见反馈 帮助中心 APP下载
官方微信

举报

0/150
提交
取消