将iOS的,如果它与UIBackgroundModes位置登记醒来终止应用程序? [英] Will iOS wake up the terminated app if it's registered with location for UIBackgroundModes?

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

问题描述

我知道,如果一个应用程序使用的显著变化位置服务,iOS将唤醒它,如果有要传送位置更新,即使该应用程序将被终止。

我无法找到的情况下一个明确的答案,如果应用程序是使用标准定位服务,并指定位置为UIBackgroundModes的关键:将iOS版也唤醒它为客户提供更新,即使它的终止?抑或是应用程序需要在后台运行,以获得位置更新回调?

更新:当时我问这个,我没有测试它的时间。但之后,我得到了一个答案在这里,我写了这片code在我的应用程序的委托来看看,当它得到一个位置更新我的应用程序终止将重新启动。我显示UILocalNotification当我通知了更新。然而,当我结束我的应用程序,然后在这个城市改变了我的位置,应用程序没有重新启动,我没有得到任何更新。你能告诉我什么是我做错了吗?

更新#2:根据这个Q&功放最终结果; A,没有什么不对的code和它是一个应用程序的预期行为,使用标准定位服务,而不是终止后重新启动。

我添加位置作为Info.plist文件中UIBackgroundModes之一。

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

   - (BOOL)应用:(*的UIApplication)的应用didFinishLaunchingWithOptions:(NSDictionary的*)launchOptions {    [UIApplication的sharedApplication] .applicationIconBadgeNumber = 0;    m_locManager = [[CLLocationManager的alloc]初始化];
    m_locManager.delegate =自我;
    [m_locManager startUpdatingLocation]
    返回YES;
} - (无效)applicationDidEnterBackground:(UIApplication的*){应用
    [m_locManager startUpdatingLocation]
} - (无效)的LocationManager:(CLLocationManager *)经理didFailWithError:(NSError *)错误
{
    的NSLog(%@,[的NSString stringWithFormat:@背景失败%@,[错误localizedDescription]]);
} - (无效)的LocationManager:(CLLocationManager *)经理
    didUpdateToLocation:(CLLocation *)newLocation
           fromLocation:(CLLocation *)oldLocation
{
    UILocalNotification * theNotification = [[UILocalNotification的alloc]初始化];
    theNotification.alertBody = [的NSString stringWithFormat:@背景位置%.06f%.06f%@,newLocation.coordinate.latitude,newLocation.coordinate.longitude,newLocation.timestamp]
    theNotification.alertAction = @确定;    theNotification.fireDate = [的NSDate dateWithTimeIntervalSinceNow:1];    [UIApplication的sharedApplication] scheduleLocalNotification:theNotification];
}


解决方案

是的,它的行为方式相同,除了它就会更经常收到更新(从而消耗更多的电池)。

来源:<一个href=\"http://developer.apple.com/library/ios/#DOCUMENTATION/iPhone/Conceptual/iPhoneOSProgrammingGuide/ManagingYourApplicationsFlow/ManagingYourApplicationsFlow.html\"相对=nofollow>应用国和多任务下的部分的的>跟踪用户位置

修改重新阅读后,似乎该计划将从暂停的状态被唤醒,但不是的终止的之一。我不认为你真的需要这种背景模式,虽然。它是专为那些需要精确的位置信息(如转由转动导航)应用程序。踩点编程指南关于区域型编程节。在您的意见的例子被列为该应用程序的用途之一。

再次编辑按照注释中的讨论,最终的解决方案似乎是设置显著位置的变化,直到用户足够接近,然后切换到精确的位置变化(与希望的应用在这段时间^^不会被终止)

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.

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?

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?

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.

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).

Source: App States and Multitasking under the section Tracking the User's Location.

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 ^^)

这篇关于将iOS的,如果它与UIBackgroundModes位置登记醒来终止应用程序?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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