首页 文章

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

提问于
浏览
1

我有一个关键问题让我与你分享 . 我有一个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

    看看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:
    
  • 2

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

    有关更多信息,请阅读Apple's Location Awareness Programming Guide . 祝好运 .

  • 0

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

    一些说明:

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

    • 更好用:

    [locationManager startMonitoringSignificantLocationChanges];

    这样功耗就少得多(即使精度较低......)

    • 使用区域:即使您的应用被杀死/终止,也会遵守这些区域 . (我们在io5中进行了测试,它的工作原理)

相关问题