如果已为 UIBackgroundModes 注册了位置,iOS 会唤醒已终止的应用程序吗? [英] Will iOS wake up the terminated app if it's registered with location for UIBackgroundModes?

查看:20
本文介绍了如果已为 UIBackgroundModes 注册了位置,iOS 会唤醒已终止的应用程序吗?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我知道如果一个应用程序使用重大变化的位置服务",如果有一个位置更新要传递,iOS 会唤醒它,即使应用程序被终止.

I know that if an app uses "The significant-change location service", iOS will wake it up if there's a location update to be delivered, even if the app is terminated.

如果应用程序使用标准位置服务并将位置指定为 UIBackgroundModes 的键,我无法找到关于此案的明确答案:即使它被终止,iOS 也会唤醒它以提供更新吗?还是应用需要在后台运行才能获取位置更新回调?

I couldn't find a clear answer about the case if the app is using standard location services and specifies location as the key for UIBackgroundModes: Will iOS also wake it up to deliver the update even if it's terminated? Or does the app need to be running in the background to get the location update callback?

更新:在我提出这个问题时,我没有时间测试它.但是在我在这里得到答案之后,我在我的应用程序的委托中编写了这段代码,以查看我终止的应用程序是否会在它获得位置更新时重新启动.当我收到更新通知时,我正在显示 UILocalNotification.但是,当我终止我的应用程序然后更改我在城市中的位置时,该应用程序没有重新启动,我也没有得到任何更新.你能告诉我我做错了什么吗?

UPDATE: At the time I was asking this, I didn't have the time to test it. But after I got an answer here, I wrote this piece of code in my application's delegate to see if my terminated app will be relaunched when it gets a location update. I'm displaying a UILocalNotification when I'm notified of the update. However, when I terminated my app and then changed my location in the city, the app was not relaunched and I didn't get any updates. Can you tell me what it's that I'm doing wrong?

更新#2:根据本问答中的最终调查结果,此代码没有任何问题,这是使用标准位置服务的应用程序的预期行为,之后不会重新启动终止.

UPDATE #2: According to the final findings in this Q&A, there's nothing wrong with this code and it's the expected behaviour for an app that uses standard location services, not to be relaunched after termination.

我已将位置添加为 Info.plist 文件中的 UIBackgroundMode 之一.

I've added location as one of the UIBackgroundModes in the Info.plist file.

这是我的应用委托的位置相关部分:

And this is the location related parts of my app delegate:

- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {    

    [UIApplication sharedApplication].applicationIconBadgeNumber = 0;

    m_locManager = [[CLLocationManager alloc] init];
    m_locManager.delegate = self;
    [m_locManager startUpdatingLocation];
    return YES;
}

- (void)applicationDidEnterBackground:(UIApplication *)application {
    [m_locManager startUpdatingLocation];
}

- (void)locationManager:(CLLocationManager *)manager didFailWithError:(NSError *)error
{
    NSLog("%@", [NSString stringWithFormat:@"Background Fail %@", [error localizedDescription]]);
}

- (void)locationManager:(CLLocationManager *)manager
    didUpdateToLocation:(CLLocation *)newLocation
           fromLocation:(CLLocation *)oldLocation
{
    UILocalNotification * theNotification = [[UILocalNotification alloc] init];
    theNotification.alertBody = [NSString stringWithFormat:@"Background location %.06f %.06f %@" , newLocation.coordinate.latitude, newLocation.coordinate.longitude, newLocation.timestamp];
    theNotification.alertAction = @"Ok";

    theNotification.fireDate = [NSDate dateWithTimeIntervalSinceNow:1];

    [[UIApplication sharedApplication] scheduleLocalNotification:theNotification];
}

推荐答案

是的,它的行为方式相同,只是它会更频繁地接收更新(因此会消耗更多电池).

Yes, it will behave the same way except it will receive updates more often (and thus consume more battery).

来源:应用程序状态和多任务处理跟踪用户位置部分下.

EDIT 重新阅读后,似乎程序将从 suspended 状态而不是 terminate 状态唤醒.不过,我不认为你真的需要这种后台模式.它专为需要精细位置信息的应用程序(如逐向导航)而设计.查看位置编程指南中有关基于区域"编程的部分.您评论中的示例被列为此应用的用途之一.

EDIT After re-reading, it seems that the program will be woken up from a suspended state but not a terminated one. I don't think you really need this background mode, though. It is designed for apps that need fine location info (like turn-by-turn navigation). Check out the section in the Location Programming guide about "region based" programming. The example in your comments is listed as one of the uses of this app.

再次编辑根据评论中的讨论,最终的解决方案似乎是设置显着的位置变化,直到用户足够近,然后切换到精细的位置变化(并希望应用在此期间不会终止 ^^)

EDIT AGAIN As per the discussion in the comments, the final solution seems to be setting significant location change until the user is close enough, and then switching over to fine location change (and hoping the app doesn't get terminated during that time ^^)

这篇关于如果已为 UIBackgroundModes 注册了位置,iOS 会唤醒已终止的应用程序吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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