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

Paste_Image.png

第二步,在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

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

  1. iOS后台定位实现

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

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

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

  3. iOS:后台定位并实时向服务器发送位置

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

  4. iOS 后台定位

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

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

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

  6. 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 ...

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

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

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

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

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

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

随机推荐

  1. BZOJ2652 : 三角板

    首先旋转坐标系,假设$(x,y)$被$(X,Y)$遮挡等价于$X\leq x$且$Y\leq y$. 对于每种坐标系建立两棵线段树: 第一棵按$x$维护已经加入的点的$y$的最小值: 第二棵按$x$维 ...

  2. Storm-166:Nimbus HA solution based on Zookeeper

    Nimbus HA feature is quite important for our application running on the storm cluster. So, we've bee ...

  3. 移动端:active,:hover无法很好触发动画的解决方案

    移动端:active,:hover无法很好触发动画的解决方案 1:问题环境: 用css3定义了一个动画,使用:hover伪类调用动画时在移动端不能很好的进行动画. 2:解决方案: 定义一个open类, ...

  4. Facebook React.js库 入门实例教程

    作者: 阮一峰 日期: 2015年3月31日 现在最热门的前端框架,毫无疑问是 React . 上周,基于 React 的 React Native 发布,结果一天之内,就获得了 5000 颗星,受瞩 ...

  5. [Leetcode] Merge Intevals

    Question: Given a collection of intervals, merge all overlapping intervals. For example,Given [1,3], ...

  6. 【BZOJ】2924: [Poi1998]Flat broken lines

    题意 平面上有\(n\)个点,如果两个点的线段与\(x\)轴的角在\([-45^{\circ}, 45^{\circ}]\),则两个点可以连线.求最少的折线(折线由线段首尾相连)使得覆盖所有点. 分析 ...

  7. TCP连接探测中的Keepalive和心跳包

    TCP连接探测中的Keepalive和心跳包 tcp keepalive 心跳 保活 Linuxtcp心跳keepalive保活1. TCP保活的必要性 1) 很多防火墙等对于空闲socket自动关闭 ...

  8. 树莓派安装LAMP作为服务器

    先运行 sudo apt-get update 更新软件源 1.安装Apache sudo apt-get install apache2 2.安装Mysql sudo apt-get install ...

  9. iis中MIME类型的介绍与使用

    今天在服务器上碰到由.mp3格式转化生成的.m4r格式不能被浏览器访问(MP3与m4r在同个域名目录下eg:www.abc.com/1.m4r) 解决办法: 1.选中文件所在的站点: 2.找到MIME ...

  10. Radar Installation

    Radar Installation 题目链接:http://acm.hust.edu.cn/vjudge/contest/view.action?cid=86640#problem/C 题目: De ...