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

第一步,开启后台模式,选中定位,选择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] > 8)
    {
         /** 请求用户权限:分为:只在前台开启定位  /在后台也可定位, */

         /** 只在前台开启定位 */
//        [self.locationManager requestWhenInUseAuthorization];

         /** 后台也可以定位 */
        [self.locationManager requestAlwaysAuthorization];
    }

    if ([[UIDevice currentDevice].systemVersion floatValue] > 9)
    {
         /** 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:0];

    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
时间: 2024-12-05 19:43:11

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

iOS后台定位实现

iOS后台定位实现 (2013-01-24 16:43:12) 工作中碰到一个定位的应用场景:app需要在后台运行,实时上传用户地理位置. 苹果对iOS的规范性在提升了app的品质的同时也带来了对开发者的限制.翻阅了各种官方文档和资料,得出结论如下: 1.实现后台定位有2种方式: standard location service(调用CLLocationManager的startUpdatingLocation) significant-change location service(调用CL

iOS 后台定位被拒注意事项

iOS 后台定位被拒的原因很简单就是没有达到苹果对后台定位的要求. 本地要求: 1.在plist文件中添加字段 "Privacy - Location Always Usage Description" ,值就填写在你的运用程序中使用后台定位的地方.如"高德地图会在搜索和导航服务中使用你的位置信息." 2.在plist文件中添加字段 "Required background modes" 数组类型添加一个值item 值的话就填 "App

iOS 后台定位

http://www.cocoachina.com/ios/20150724/12735.html 前言 之前的文章说过 我现在做的是LBS定位的社交APP 其中主要的一个功能就是能够实时定位社交圈中各个成员的位置 后台实时上传位置则是非常重要的一个技术点 接下来就来说说我关于这方面的实践经验 需求 先来看看实现这个功能的具体需求是什么 由于我们是实时定位的生活类社交APP 所以我们需要做到一下几点 1. 如果用户的位置在持续变化 则隔一段时间上报一次 由于我们希望能够实时的将用户的位置变化反馈

iOS后台定位

iOS地理定位 app正常运行时可以,按下home键后app在后台也可以,双击home键后台杀死app也可以,甚至重启机器后也可以.(iOS 10 测试代码) 1)设置一些请求参数 就像正常的CLLocationManager一样申请权限以及后台更新请求 后台更新: plist请求地理位置(需要跑始终使用) 2)与往常一样的初始化定位管理器等步骤 CLLocationManager *locationMgr = [[CLLocationManager alloc]init]; [location

iOS网络开发基础--向服务器发送数据的方式

可以通过三种方式向服务器发送数据:NSURLRequest,NSMutableURLRequest,NSURLConnection 一.NSURLRequest向服务器发送同步或异步请求 举例:如何发送一个GET请求 * 默认就是GET请求 // 1.URL NSURL *url = [NSURL URLWithString:@"http://www.baidu.com"]; // 2.请求 NSURLRequest *request = [NSURLRequest requestWi

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 the use, using mechanisms described in the Human Interface Guidelines 4.5 Details Your app uses background location services but does not clarify the pu

避开ie缓存机制,实时从服务器取得最新数据

HTML页面head头部添加如下代码 <meta http-equiv="Pragma" content="no-cache">    <meta http-equiv="Cache-Control" content="no-cache"> <meta http-equiv="Expires" content="0"> Ajax发送请求时候URL跟上一

iOS 9适配系列教程:后台定位

http://www.cocoachina.com/ios/20150624/12200.html Demo:GitHub地址 [iOS9在定位的问题上,有一个坏消息一个好消息]坏消息:如果不适配iOS9,就不能偷偷在后台定位(不带蓝条,见图)!好消息:将允许出现这种场景:同一App中的多个location manager:一些只能在前台定位,另一些可在后台定位,并可随时开启或者关闭特定location manager的后台定位. 如果没有请求后台定位的权限,也是可以在后台定位的,不过会带蓝条:

iOS多线程与网络开发之发送接收服务器信息

郝萌主倾心贡献,尊重作者的劳动成果,请勿转载. 如果文章对您有所帮助,欢迎给作者捐赠,支持郝萌主,捐赠数额随意,重在心意^_^ 我要捐赠: 点击捐赠 Cocos2d-X源码下载:点我传送 游戏官方下载:http://dwz.cn/RwTjl 游戏视频预览:http://dwz.cn/RzHHd 游戏开发博客:http://dwz.cn/RzJzI 游戏源码传送:http://dwz.cn/Nret1 A.搭建java服务器 使用eclipse.tomcat和struts2框架搭建一个简单的服务器