• 第一步:开启后台模式,选中定位,选择project --> capabilities-->Backgorund Modes --> Location updates 如图:

  • 第二步:在info.list 文件中添加如下配置
允许 http 请求 ,ios 9 之后需要添加,便于向服务器发送请求
<key>NSAppTransportSecurity</key>
<dict>
<key>NSAllowsArbitraryLoads</key>
<true/>
</dict>
添加定位权限,ios8之后需要添加,否则无法定位
<key>NSLocationWhenInUseUsageDescription</key>
<string>YES</string>
<key>NSLocationAlwaysUsageDescription</key>
<string>YES</string>
  • 第三步:代码如下
#import "ViewController.h"
@interface ViewController ()
@end @implementation ViewController
- (void)viewDidLoad {
[super viewDidLoad];
// Do any additional setup after loading the view, typically from a nib.
self.view.backgroundColor = [UIColor whiteColor];
self.title = @"后台定位";
self.locationManager = [[CLLocationManager alloc] init];
self.locationManager.delegate = self;
[self.locationManager setDesiredAccuracy:kCLLocationAccuracyBest];
if ([[UIDevice currentDevice].systemVersion floatValue] > ) {
/** 请求用户权限:分为:只在前台开启定位 /在后台也可定位, */
/** 只在前台开启定位 */
// [self.locationManager requestWhenInUseAuthorization];
/** 后台也可以定位 */
[self.locationManager requestAlwaysAuthorization];
}
if ([[UIDevice currentDevice].systemVersion floatValue] > ) {
/** iOS9新特性:将允许出现这种场景:同一app中多个location manager:一些只能在前台定位,另一些可在后台定位(并可随时禁止其后台定位)。 */
[self.locationManager setAllowsBackgroundLocationUpdates:YES];
}
/** 开始定位 */
[self.locationManager startUpdatingLocation];
}
#pragma mark - 定位代理方法
- (void)locationManager:(CLLocationManager *)manager didUpdateLocations:(NSArray<CLLocation *> *)locations {
CLLocation *loc = [locations objectAtIndex:];
NSLog(@"经纬度 %f %f ",loc.coordinate.latitude,loc.coordinate.longitude);
NSURLSession *session = [NSURLSession sharedSession];
NSURLSessionDataTask *task = [session dataTaskWithRequest:[NSURLRequest requestWithURL:[NSURL URLWithString:@"http://ac.ybjk.com/ua.php"]] completionHandler:^(NSData * _Nullable data, NSURLResponse * _Nullable response, NSError * _Nullable error) {//
//NSLog(@"response %@",response);
NSString *result = [[NSString alloc] initWithData:data encoding:NSUTF8StringEncoding];
NSLog(@"result %@",result);
}];
[task resume];
}
- (void)didReceiveMemoryWarning {
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
}
@end

至此,完成后台实时定位功能,并向服务器发送请求成功。

为原博主点赞吧:http://www.jianshu.com/p/0b339f1ff894

iOS:后台定位并实时向服务器发送位置的更多相关文章

  1. iOS后台定位,实时向服务器发送最新位置

    第一步,开启后台模式,选中定位,选择project --> capabilities-->Backgorund Modes --> Location updates 如图: Past ...

  2. iOS后台定位实现

    iOS后台定位实现 (2013-01-24 16:43:12)     工作中碰到一个定位的应用场景:app需要在后台运行,实时上传用户地理位置.   苹果对iOS的规范性在提升了app的品质的同时也 ...

  3. iOS 后台定位被拒注意事项

    iOS 后台定位被拒的原因很简单就是没有达到苹果对后台定位的要求. 本地要求: 1.在plist文件中添加字段 "Privacy - Location Always Usage Descri ...

  4. iOS 后台定位

    http://www.cocoachina.com/ios/20150724/12735.html 前言 之前的文章说过 我现在做的是LBS定位的社交APP 其中主要的一个功能就是能够实时定位社交圈中 ...

  5. ios-高德、百度后台定位并上传服务器

    一.配置高德或百度的后台定位框架和代码(略). 二.配置app不被系统kill,定时获取地理位置信息,并上传服务器(AppDelegate里面). 具体代码: 1. - (void)applicati ...

  6. iOS后台定位时授权提示一闪而过的解决办法

    今天做后台定位时,授权提示"允许 XXX 在您并未使用该应用时范文您的位置吗?"总是一闪而过,点不到,或者压根就不弹出.后来找到了解决问题的方法,那就是:将CLLocationMa ...

  7. iOS 后台定位审核被拒How to clarify the purpose of its use in the locatio

    4.5 - Apps using background location services must provide a reason that clarifies the purpose of th ...

  8. iOS开发雕虫小技之傻瓜式定位神器-超简单方式解决iOS后台定时定位

    1.概述 由于公司一款产品的需求,最近一直在研究iOS设备的后台定位.主要的难点就是,当系统进入后台之后,程序会被挂起,届时定时器.以及代码都不会Run~ 所以一旦用户将我的App先换到了后台,我的定 ...

  9. iOS 后台持续定位详解(支持ISO9.0以上)

    iOS 后台持续定位详解(支持ISO9.0以上) #import <CoreLocation/CoreLocation.h>并实现CLLocationManagerDelegate 代理, ...

随机推荐

  1. Java 读写图像

    Java中进行图像I/O(即读图片和写图片,不涉及到复杂图像处理)有三个方法:1. Java Image I/O API,支持常见图片,从Java 2 version 1.4.0开始就内置了.主页:h ...

  2. JBPM表达业务流程(流程定义语言)

    业务流程包括三部分: 活动 Activity / 节点 Node (有很多种,不同的类型有不同的功能,必须要有一个Start Activity) 连线 Transition / 转移 (从一个Acti ...

  3. 象写程序一样写博客:搭建基于github的博客

    象写程序一样写博客:搭建基于github的博客   前言 github 真是无所不能.其 Pages 功能 支持上传 html,并且在页面中显示.于是有好事者做了一个基于 github 的博客管理工具 ...

  4. Angularjs - 路由 angular-ui-router

    注意,使用的路由不是官方的,而是第三方的.因为这个更加强大支持嵌套而且大家都是这样用的 http://www.tuicool.com/articles/zeiy6ff http://www.open- ...

  5. codeacademy

    codeacademy http://link.zhihu.com/?target=https%3A//www.youtube.com/playlist%3Flist%3DPLP6DbQBkn9ymG ...

  6. SQL判断语句用法和多表查询

    1.格式化时间sql语句 本例中本人随便做了两张表,和实际不是很相符,只是想说明sql语句的写法. 例1表格式如下: 需求:查询出本表,但需要使time字段的时间格式为yyyy-MM-dd,比如:20 ...

  7. Bootstrap兼容

    转:http://blog.csdn.net/chenhongwu666/article/details/41513901 如有雷同,不胜荣幸,若转载,请注明让IE6 IE7 IE8 IE9 IE10 ...

  8. Data Plane Development Kit (DPDK): Getting Started

    参考:dpdk getting started 系统: Ubuntu 14.04 内核信息: 执行 uname -a Linux chen-VirtualBox 3.13.0-32-generic # ...

  9. java web基础2HTTP协议知识点总结

    一.HTTP协议基础 1.定义:HTTP是基于TCP连接的浏览器与服务器通信协议.(即传输层先用TCP三次握手建立连接,进而HTTP通信) 2.连接原理:先进行TCP建立端到端连接,然后发送和接受HT ...

  10. extjs文件上传

    EXT学习教程:http://www.cnblogs.com/iamlilinfeng/category/385121.html Ext文件上传: 例子用到的jar:   1.upload.js /* ...