iOS:后台定位并实时向服务器发送位置
- 第一步:开启后台模式,选中定位,选择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:后台定位并实时向服务器发送位置的更多相关文章
- iOS后台定位,实时向服务器发送最新位置
第一步,开启后台模式,选中定位,选择project --> capabilities-->Backgorund Modes --> Location updates 如图: Past ...
- iOS后台定位实现
iOS后台定位实现 (2013-01-24 16:43:12) 工作中碰到一个定位的应用场景:app需要在后台运行,实时上传用户地理位置. 苹果对iOS的规范性在提升了app的品质的同时也 ...
- iOS 后台定位被拒注意事项
iOS 后台定位被拒的原因很简单就是没有达到苹果对后台定位的要求. 本地要求: 1.在plist文件中添加字段 "Privacy - Location Always Usage Descri ...
- iOS 后台定位
http://www.cocoachina.com/ios/20150724/12735.html 前言 之前的文章说过 我现在做的是LBS定位的社交APP 其中主要的一个功能就是能够实时定位社交圈中 ...
- ios-高德、百度后台定位并上传服务器
一.配置高德或百度的后台定位框架和代码(略). 二.配置app不被系统kill,定时获取地理位置信息,并上传服务器(AppDelegate里面). 具体代码: 1. - (void)applicati ...
- iOS后台定位时授权提示一闪而过的解决办法
今天做后台定位时,授权提示"允许 XXX 在您并未使用该应用时范文您的位置吗?"总是一闪而过,点不到,或者压根就不弹出.后来找到了解决问题的方法,那就是:将CLLocationMa ...
- 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 ...
- iOS开发雕虫小技之傻瓜式定位神器-超简单方式解决iOS后台定时定位
1.概述 由于公司一款产品的需求,最近一直在研究iOS设备的后台定位.主要的难点就是,当系统进入后台之后,程序会被挂起,届时定时器.以及代码都不会Run~ 所以一旦用户将我的App先换到了后台,我的定 ...
- iOS 后台持续定位详解(支持ISO9.0以上)
iOS 后台持续定位详解(支持ISO9.0以上) #import <CoreLocation/CoreLocation.h>并实现CLLocationManagerDelegate 代理, ...
随机推荐
- HttpRuntime.Cache的使用经验
配置文件 <appSettings> <add key="EnableCache" value="true"/> "/ ...
- Angular数据双向绑定
Angular数据双向绑定 AngularJS诞生于2009年,由Misko Hevery 等人创建,后为Google所收购.是一款优秀的前端JS框架,已经被用于Google的多款产品当中.Angul ...
- echo "不允许上传该类型的文件
<?php教程 // 上传设置 $maxsize=10002400; //最大允许上传的文件大小 $alltype=array(".php"," ...
- String对象的方法
Function Description Example CompareTo() Compares one string to another ("Hello").CompareT ...
- linphone3.4.0代码分析
主要类型定义: 1.LinphoneCoreVTable /** * This structure holds all callbacks that the application should im ...
- linux消息队列的使用及内核实现原理
mq_receive NAME mq_open - open a message queue SYNOPSIS #include <fcntl.h> /* For O_* constant ...
- DS实验题 order
算法与数据结构 实验题 6.4 order ★实验任务 给出一棵二叉树的中序遍历和每个节点的父节点,求这棵二叉树的先序和后序遍历. ★数据输入 输入第一行为一个正整数n表示二叉树的节点数目,节点编号从 ...
- MySQL 数据库设计 笔记与总结(4)维护优化
[维护和优化的工作] ① 维护数据字典 ② 维护索引 ③ 维护表结构 ④ 在适当的时候对表进行水平拆分或垂直拆分 [维护数据字典] a 使用第三方工具对数据字典进行维护 b 利用数据库本身的备注字段来 ...
- Yii源码阅读笔记(八)
前面阅读了Yii2的两个基本类Object和Component,了解了Yii的三个重要概念属性.事件.行为,下面开始阅读Event类,Event类是所有事件类的基类: <?php /** * @ ...
- memcached学习笔记5--socke操作memcached 缓存系统
使用条件:当我们没有权限或者不能使用服务器的时候,我们需要用socket操作memcached memcached-client操作 特点: 无需开启memcache扩展 使用fsocketopen( ...