如何永远在后台模式下运行应用程序?

时间:2011-09-16 06:48:57

标签: core-location cllocationmanager

我遇到了一个关键问题让我与你分享。 我有一个iPhone应用程序需要将当前位置发送到服务器,无论应用程序是在每分钟的背景模式还是前台模式。 我完成了在info.plist参数中设置属性,如后台模式 - >位置和其他位置配置代码通常

- (CLLocationManager *)loc_mngr {

    if (loc_mngr != nil) {
        return loc_mngr;
    }


    loc_mngr = [[CLLocationManager alloc] init];
    loc_mngr.desiredAccuracy = kCLLocationAccuracyNearestTenMeters;
    loc_mngr.delegate = self;
    return loc_mngr;

}

loc_mngr是CLLocationManager的一个对象。 委托方法实现完成。 它适用于我,但问题是当我的应用程序在后台时如何启用handel位置服务。 请帮帮我怎么处理? 谢谢你。

3 个答案:

答案 0 :(得分:2)

看一下this apple document,你有以下方法来做这件事。

CLLocationCoordinate2D coord = CLLocationCoordinate2DMake(0.0, 0.0); 
CLRegion *region = [[CLRegion alloc] initWithCircularRegionWithCenter:coord radius:500 identifier:@"Some Identifier"];

然后,使用框架注册该区域:

[locationManager startMonitoringForRegion:region]; 

委托协议定义了三种用于区域的方法:

– locationManager:didEnterRegion:
– locationManager:didExitRegion:
– locationManager:monitoringDidFailForRegion:withError:

答案 1 :(得分:0)

您需要创建一个CLLocationManagerDelegate来接收应用程序的回调。您需要设置要运行的位置项,当应用程序转到后台时,您需要让代理“监听”位置更新。这可以通过区域监控-didEnterRegion或-didExitRegion来实现。或者像标准位置监控一样简单-didUpdateToLocation。

有关详细信息,请阅读Apple's Location Awareness Programming Guide。祝你好运。

答案 2 :(得分:0)

术语“永远”在概念上是错误的:Apple保留随时杀死应用程序的权利。你需要重新考虑逻辑。

一些注意事项:

  • 始终保持本地化(标准化本地化)是错误的:它非常快速地消耗电池

  • 最好使用:

    [locationManager startMonitoringSignificantLocationChanges];

    这种方式功耗要低得多(即使精度较低......)

  • 使用区域:即使您的应用被杀死/终止,也会遵守这些区域。 (我们在io5中进行了测试,它确实有效)