实时向服务器发送最新位置

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

第一步,开启后台模式,选中定位,选择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];   � 
    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];
 �}   
@end
�```
至此,完成后台实时定位功能,并向服务器发送请求成功!
最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容

  • 第一步,开启后台模式,选中定位,选择project --> capabilities-->Backgorund M...
    smile小芳阅读 1,309评论 1 2
  • Spring Cloud为开发人员提供了快速构建分布式系统中一些常见模式的工具(例如配置管理,服务发现,断路器,智...
    卡卡罗2017阅读 135,026评论 19 139
  • 发现 关注 消息 iOS 第三方库、插件、知名博客总结 作者大灰狼的小绵羊哥哥关注 2017.06.26 09:4...
    肇东周阅读 12,261评论 4 61
  • 今天是开学第一天,班主任周老师把我们全班同学分成八个小队,我作为我们小队的一份子,必须要给我们小队增光添...
    杨浩然五年级三班阅读 200评论 1 1
  • ② 诶??? 小嘟老师愣了一下,暗想这个人怎么这么自来熟。但面上却是笑着应了句:“内!很好吃!” 这倒不是恭维,煮...
    咧咧和咻咻阅读 347评论 0 0