15分钟后背景位置更新不适用于ios?

问题描述:

我已经在基于定时器的ios中实现了背景位置更新(如每1分钟)。它的工作良好,并获得位置(纬度和长度值),但15后它断开连接。 我无法触发它了。 任何人都可以帮助我。15分钟后背景位置更新不适用于ios?

这是我的代码..提前

- (void)applicationDidEnterBackground:(UIApplication *)application { 

    [[GlobalClass sharedGlobals].locationManager stopMonitoringSignificantLocationChanges]; 

    if ([[[UIDevice currentDevice] systemVersion] floatValue] >= 8) { 
     [[GlobalClass sharedGlobals].locationManager requestAlwaysAuthorization]; 
    } 
    [[GlobalClass sharedGlobals].locationManager startMonitoringSignificantLocationChanges]; 

    int timeInterval = 60; // (i.e., 1 mins) 

    if (!setTimer) { 
     setTimer = [NSTimer scheduledTimerWithTimeInterval:timeInterval target: self 
                selector: @selector(toMakeServerCall) userInfo: nil repeats: YES]; 
    } 
} 

#pragma mark - Send Current lat & long to the server 

- (void)toMakeServerCall { 

    NSString *latitude = [NSString stringWithFormat:@"%f",[[GlobalClass sharedGlobals] currentLocationObject].coordinate.latitude]; 
    NSString *longitude = [NSString stringWithFormat:@"%f",[[GlobalClass sharedGlobals] currentLocationObject].coordinate.longitude]; 

    NSLog(@"Current Lat is :%@ \n Current Long is :%@ ",latitude,longitude); 

    NSURL *url = [NSURL URLWithString:[NSString stringWithFormat:@"http:www.google.com/myLatitude is: %@ and My Longitude is: %@",latitude,longitude]]; 

    NSURLRequest *request = [NSURLRequest requestWithURL:url]; 

} 

感谢。

当您使用“重大更改”服务(或坦率地说任何位置服务)时,您不使用计时器。当地点发生变化时,操作系统会呼叫,而不是相反。

关于你的计时器,当应用程序暂停时,定时器将不再运行。所以,不要使用计时器。但有了重大的变更服务,当设备检测到它已经移动了一段很长的距离时,我们会唤醒您的应用并通知您。正如Location and Maps Programming Guide: Starting the Significant-Change Location Service说:

如果离开了显著变化位置服务运行和iOS应用随后被暂停或终止,在新的位置数据到达该服务会自动唤醒你的应用程序。在起床时间,应用程序被置于后台,您可以获得少量时间(大约10秒钟)以手动重新启动位置服务并处理位置数据。 (如Knowing When to Start Location Services中所述,您必须在后台手动重新启动位置服务,然后才能传递任何未决的位置更新。)由于您的应用程序位于后台,因此它必须执行最少的工作并避免任何任务(如查询网络)可能会阻止它在分配的时间到期之前返回。如果没有,你的应用程序将被终止。如果iOS应用程序需要更多时间来处理位置数据,则可以使用UIApplication类的beginBackgroundTaskWithName:expirationHandler:方法请求更多后台执行时间。

+0

但是,应用程序仍然不会每分钟运行。即使存在重要的位置更新,重大的位置更改也会在10到15分钟内最多更新几次。有没有其他方法可以继续在后台运行应用程序,并且每秒更新一次到我的服务器的位置?就好像我想通过更新服务器上的位置来准确跟踪汽车所经过的路线。 –

+0

首先,每秒都不会发生显着变化,因为根据我的经验,您需要实现的距离很远(例如不是米,而是公里)。如果您想在汽车移动时不断更新,则必须(a)在功能屏幕中注册背景导航模式;和(b)然后使用标准的位置服务。但是,您再也不使用定位服务的定时器。定位服务会告诉您何时已经遍历了足够的距离以触发位置更新。 – Rob

+0

感谢@Rob。我的实际要求是,我需要在动态时间间隔(如1小时,1天,3天等)期间在后台/应用程序终止状态期间更新位置。在那种情况下,我需要做什么?如果可能的话,你能分享一下示例代码吗? – Surezz

@surezz:请参考下面的URL我的答案。可能会帮助你。

Location URL