发送纬度和经度到服务器时,应用程序在后台 [英] Sending Latitude and Longitude to Server when app is in background

查看:136
本文介绍了发送纬度和经度到服务器时,应用程序在后台的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我经历了这么多的环节不见了,甚至之后我还没有找到获取经纬度妥善的解决办法。

周期性的iOS背景更新位置

<一个href=\"http://stackoverflow.com/questions/15650139/ios-long-running-background-timer-with-location-background-mode\">iOS长期运行的背景定时器&QUOT;地点&QUOT;背景模式

我从一些链接和论坛尝试,但它是工作仅3分钟,然后应用完全不更新用户的位置。

   - (无效)applicationDidEnterBackground:(UIApplication的*){应用
//使用这个方法来释放共享资源,保存用户数据,无效定时器,并储存足够的应用程序状态信息到你的应用程序恢复到其当前状态的情况下,它以后终止。
//如果你的应用程序支持后台运行,这种方法被调用,而不是applicationWillTerminate:当用户退出。
//创建新uiBackgroundTask
__block UIBackgroundTaskIdentifier bgTask = [应用beginBackgroundTaskWithExpirationHandler:^ {
    [应用endBackgroundTask:bgTask];
    bgTask = UIBackgroundTaskInvalid;
}];//并创建异步调用新的计时器:
dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_HIGH,0),^ {    [的LocationManager startUpdatingLocation]    的NSTimer * T = [的NSTimer scheduledTimerWithTimeInterval:10目标:自我选择:@selector(startTrackingBg)USERINFO:无重复:YES];
    [NSRunLoop currentRunLoop] addTimer:T已forMode:NSDefaultRunLoopMode];
    [NSRunLoop currentRunLoop]运行]。
});}
  - (无效)的LocationManager:(CLLocationManager *)经理didUpdateLocations:(NSArray的*)的位置
{
//存储数据
CLLocation * newLocation = [位置lastObject]
//告诉您要推迟这个updatedLocation的centralManager
如果(_isBackgroundMode&安培;&安培;!_deferringUpdates)
{
    _deferringUpdates = YES;
    [的LocationManager allowDeferredLocationUpdatesUntilTraveled:CLLocationDistanceMax超时:10];
}
}


解决方案

确定。

挣扎持续3天之后,将其工作对我发送纬度和经度时应用即使经过3分钟为背景。

我检查我的应用程序,不断在后台Lat Long网发送了一个多小时。

这至少可以帮助一些之一。

首先,请在你的plist中添加下面的两个键。

  1.NSLocationAlwaysUsageDescription
 2.NSLocationWhenInUseUsageDescription

博特是字符串,你可以给任意值。

那么请打开后台获取并根据项目部分功能检查位置更新。

然后导入Corelocation框架,并添加此下方code。

的LocationManager是一个全局变量。

   - (BOOL)应用:(*的UIApplication)的应用didFinishLaunchingWithOptions:(NSDictionary的*)launchOptions {
//覆盖点后,应用程序启动定制。
 //创建CLLocationManager变量
的LocationManager = [[CLLocationManager的alloc]初始化];
//设置委托
locationManager.delegate =自我;应用= [UIApplication的sharedApplication]
//这是为管理者设定的最重要的特性。它最终确定管理者将如何
//试图获取位置,因此,将要消耗的功率量。如果([的LocationManager respondsToSelector:@selector(setAllowsBackgroundLocationUpdates :)]){
    [的LocationManager setAllowsBackgroundLocationUpdates:YES];
}
locationManager.desiredAccuracy = 45;
locationManager.distanceFilter = 100;
//一旦配置完成,外景经理必须是已启动。
[的LocationManager startUpdatingLocation]
}  - (无效)applicationWillResignActive:(UIApplication的*){应用
//当发送应用程序即将从活动转移到非活动状态。这可对某些类型的临时中断的发生(例如呼入电话呼叫或SMS消息),或当用户退出该应用程序和它开始过渡到背景状态。
//使用这个方法来暂停正在进行的任务,禁用定时器和油门下的OpenGL ES的帧速率。游戏应该使用这种方法来暂停游戏。[的LocationManager stopUpdatingLocation]
[的LocationManager setDesiredAccuracy:kCLLocationAccuracyBest];
[的LocationManager setDistanceFilter:kCLDistanceFilterNone];
locationManager.pausesLocationUpdatesAutomatically = NO;
locationManager.activityType = CLActivityTypeAutomotiveNavigation;
[的LocationManager startUpdatingLocation]
 }  - (无效)applicationDidEnterBackground:(UIApplication的*){应用
//使用这个方法来释放共享资源,保存用户数据,无效定时器,并储存足够的应用程序状态信息到你的应用程序恢复到其当前状态的情况下,它以后终止。
//如果你的应用程序支持后台运行,这种方法被调用,而不是applicationWillTerminate:当用户退出。
[的LocationManager stopUpdatingLocation]__block UIBackgroundTaskIdentifier bgTask = [应用beginBackgroundTaskWithExpirationHandler:^ {
    bgTask = UIBackgroundTaskInvalid;
}];*的NSTimer计时器= [的NSTimer scheduledTimerWithTimeInterval:10.0
                                              目标:自我
                                            选择:@selector(startTrackingBg)
                                            用户信息:无
                                             重复:YES];
 } - (无效){startTrackingBg[的LocationManager startUpdatingLocation]
 的NSLog(@应用程序在后台运行);
}
 //使用的LocationManager自动启动
   - (无效)的LocationManager:(CLLocationManager *)经理didUpdateToLocation:(CLLocation *)newLocation fromLocation:(CLLocation *)oldLocation {
纬度= newLocation.coordinate.latitude;
经度= newLocation.coordinate.longitude;
的NSLog(@位置:%F,%F,newLocation.coordinate.longitude,newLocation.coordinate.latitude);
 }

I have gone through so many links, even after that I haven't found a proper solution for getting latitude and longitude.

Periodic iOS background location updates

iOS long-running background timer with "location" background mode

I tried from some links and forums but it is working for only 3 mins, then app is not at all updating the user location.

- (void)applicationDidEnterBackground:(UIApplication *)application {
// Use this method to release shared resources, save user data, invalidate timers, and store enough application state information to restore your application to its current state in case it is terminated later.
// If your application supports background execution, this method is called instead of applicationWillTerminate: when the user quits.


//create new uiBackgroundTask
__block UIBackgroundTaskIdentifier bgTask = [app beginBackgroundTaskWithExpirationHandler:^{
    [app endBackgroundTask:bgTask];
    bgTask = UIBackgroundTaskInvalid;
}];

//and create new timer with async call:
dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_HIGH, 0), ^{

    [locationManager startUpdatingLocation];

    NSTimer* t = [NSTimer scheduledTimerWithTimeInterval:10 target:self selector:@selector(startTrackingBg) userInfo:nil repeats:YES];
    [[NSRunLoop currentRunLoop] addTimer:t forMode:NSDefaultRunLoopMode];
    [[NSRunLoop currentRunLoop] run];
});

}
 -(void) locationManager:(CLLocationManager *)manager didUpdateLocations:(NSArray *)locations
{
//  store data
CLLocation *newLocation = [locations lastObject];


//tell the centralManager that you want to deferred this updatedLocation
if (_isBackgroundMode && !_deferringUpdates)
{
    _deferringUpdates = YES;
    [locationManager allowDeferredLocationUpdatesUntilTraveled:CLLocationDistanceMax timeout:10];
}
}

解决方案

Ok.

After struggling for 3days, it is working for me for sending latitude and longitude when app is in background even after 3 mins.

I checked my app, continuously sending lat long for more than a hour in background.

It can help some one at least.

First Please add below two keys in your pList.

 1.NSLocationAlwaysUsageDescription
 2.NSLocationWhenInUseUsageDescription

Bothe are strings and you can give any value.

Then please turn on background fetch and check location updates under capabilities in project section.

Then import Corelocation framework and add this below code.

locationManager is a global variable.

- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
// Override point for customization after application launch.
 //create CLLocationManager variable
locationManager = [[CLLocationManager alloc] init];
//set delegate
locationManager.delegate = self;

app = [UIApplication sharedApplication];
// This is the most important property to set for the manager. It ultimately determines how the manager will
// attempt to acquire location and thus, the amount of power that will be consumed.

if ([locationManager respondsToSelector:@selector(setAllowsBackgroundLocationUpdates:)]) {
    [locationManager setAllowsBackgroundLocationUpdates:YES];
}
locationManager.desiredAccuracy = 45;
locationManager.distanceFilter = 100;
// Once configured, the location manager must be "started".
[locationManager startUpdatingLocation];
}

 - (void)applicationWillResignActive:(UIApplication *)application {
// Sent when the application is about to move from active to inactive state. This can occur for certain types of temporary interruptions (such as an incoming phone call or SMS message) or when the user quits the application and it begins the transition to the background state.
// Use this method to pause ongoing tasks, disable timers, and throttle down OpenGL ES frame rates. Games should use this method to pause the game.

[locationManager stopUpdatingLocation];
[locationManager setDesiredAccuracy:kCLLocationAccuracyBest];
[locationManager setDistanceFilter:kCLDistanceFilterNone];
locationManager.pausesLocationUpdatesAutomatically = NO;
locationManager.activityType = CLActivityTypeAutomotiveNavigation;
[locationManager startUpdatingLocation];
 }

 - (void)applicationDidEnterBackground:(UIApplication *)application {
// Use this method to release shared resources, save user data, invalidate timers, and store enough application state information to restore your application to its current state in case it is terminated later.
// If your application supports background execution, this method is called instead of applicationWillTerminate: when the user quits.


[locationManager stopUpdatingLocation];

__block UIBackgroundTaskIdentifier bgTask = [app beginBackgroundTaskWithExpirationHandler:^{
    bgTask = UIBackgroundTaskInvalid;
}];

NSTimer *timer = [NSTimer scheduledTimerWithTimeInterval:10.0
                                              target:self
                                            selector:@selector(startTrackingBg)
                                            userInfo:nil
                                             repeats:YES];


 }

-(void)startTrackingBg {

[locationManager startUpdatingLocation];
 NSLog(@"App is running in background");
}
 //starts automatically with locationManager
  -(void)locationManager:(CLLocationManager *)manager didUpdateToLocation:(CLLocation *)newLocation fromLocation:(CLLocation *)oldLocation{
latitude=newLocation.coordinate.latitude;
longitude=newLocation.coordinate.longitude;
NSLog(@"Location: %f, %f",newLocation.coordinate.longitude, newLocation.coordinate.latitude);
 }

这篇关于发送纬度和经度到服务器时,应用程序在后台的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆