iOS9 启动监控显著位置更改在终止时不启动应用程序?



我正在制作一个iPhone应用程序,需要将当前位置发送到服务器,以便它知道要发送哪些推送通知。它不需要非常精确,startMonitoringSignificantLocationChanges非常适合我的需要。

只要应用程序在屏幕上或后台运行,这一切都很好。然而,如果我杀死/终止应用程序,它不再工作了。从我的理解,应用程序应该自动重新启动与特殊的UIApplicationLaunchOptionsLocationKey作为启动选项。但是,应用程序不会重新启动(至少在模拟器中不会)。

我也在这里读过一些东西:当终止/暂停时,对位置API的重大更改的行为?

自动重新启动是否只在应用程序从挂起状态被系统终止时有效,而不是当你手动杀死应用程序时有效?我也尝试了特殊信息。plist UIApplicationExitsOnSuspend也会在应用进入后台时终止它。它也不会重新启动。

是否有一种方法来模拟应用程序在模拟器中被系统终止?

iOS更新后手机重启后会发生什么?是否有办法确保应用程序将重新启动?

一个应用程序被SLC重新启动,不管它是如何被杀死的,只要它被杀死时至少有一个CLLocationManager调用了startMonitoringSignificantLocationChanges。这里有一个警告-在iOS7.0(.x)版本中,它被破坏了。它在iOS7.1+中又开始工作了。

要使此工作需要完成几个步骤。

  1. 在你的项目功能中,你必须启用后台模式位置更新,因为你想在后台被唤醒。
  2. 需要在info中添加NSLocationAlwaysUsageDescription键。
  3. >>>>>>>>>>>>>>>>>>>>>>>>>
  4. 在代码中,您必须向用户请求授权以始终使用位置
  5. 在代码中,你必须请求在后台继续传递位置更新
  6. 在代码中,你必须开始监控重要的位置变化

下面是一个例子:

AppDelegate h

#import <UIKit/UIKit.h>
#import <CoreLocation/CoreLocation.h>
@interface AppDelegate : UIResponder <UIApplicationDelegate, CLLocationManagerDelegate>
@property (strong, nonatomic) UIWindow *window;
@property CLLocationManager* locationMgr;
@end

AppDelegate.m

#import "AppDelegate.h"
@implementation AppDelegate
@synthesize locationMgr;
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {    
    NSLog(@"app launching");
    locationMgr = [[CLLocationManager alloc] init];
    [locationMgr setDelegate:self];
    // Added in iOS8
    if([locationMgr respondsToSelector:@selector(requestAlwaysAuthorization)])
        [locationMgr requestAlwaysAuthorization];
    // Added in iOS9
    if([locationMgr respondsToSelector:@selector(setAllowsBackgroundLocationUpdates:)])
        [locationMgr setAllowsBackgroundLocationUpdates:YES];
    [locationMgr startMonitoringSignificantLocationChanges];
    return YES;
}
-(void) locationManager:(CLLocationManager *)manager didUpdateLocations:(NSArray *)locations
{
    NSLog(@"app delegate received significant location change");
    [[UIApplication sharedApplication] setApplicationIconBadgeNumber:[[UIApplication sharedApplication] applicationIconBadgeNumber]+1];
}
@end

当你第一次在你的设备上运行应用程序时,点击OK以允许应用程序在请求时在后台使用你的位置。然后双击home键,将应用程序从任务切换器中划掉,就可以关闭它了。点击主页,然后从屏幕底部向上滑动打开控制中心并打开飞行模式,等待几秒钟,然后关闭飞行模式。观察你的应用上的徽章计数器的增量。

它是iOS在SLC上发布的。

如果您创建两个CLLocationManager实例并调用startMonitoringSignificantLocationChanges,则调用stopMonitoringSignificantLocationChanges一个。即使另一个CLLocationManager将在应用程序继续运行时继续接收SLC事件,当应用程序因任何原因退出时,它将不会重新启动。

退出前的最后一次调用似乎设置了重新启动行为。

start, start, stop, exit -应用程序无法在SLC上重新启动。

start, start, stop start, exit -应用程序在SLC上重新启动

我已经回答了这样一个问题。你可以在下面的链接上查看我的答案。

https://stackoverflow.com/a/35722254/3368791。

最新更新