gpt4 book ai didi

background - iOS9 startMonitoringSignificantLocationChanges 终止时不启动应用程序?

转载 作者:行者123 更新时间:2023-12-04 00:50:56 24 4
gpt4 key购买 nike

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

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

我也在这里读过一些东西:
Behaviour for significant change location API when terminated/suspended?

自动重新启动是否仅在应用程序被系统从挂起状态终止时才有效,而不是在您手动终止应用程序时有效?我还尝试了特殊的 info.plist UIApplicationExitsOnSuspend,它也会在应用程序进入后台时终止它。它也不会重新启动。

有没有办法在模拟器中模拟被系统终止的应用程序?

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

最佳答案

应用程序由 SLC 重新启动,无论它是如何被杀死的,至少有一个 CLLocationManager 调用了 startMonitoringSignificantLocationChanges。对此有一个警告 - 在 iOS7.0(.x) 版本中它被破坏了。它在 iOS7.1+ 中再次开始工作。

要使其正常工作,您需要完成几个步骤。

  • 在您的项目功能中,您必须启用后台模式 位置更新因为你想在后台被唤醒。
  • 您需要添加 key NSLocationAlwaysUsageDescription 到 info.plist,其中包含为什么您希望始终能够在后台使用位置的描述。
  • 在代码中,您必须请求用户授权才能始终使用位置
  • 在代码中,您必须请求在后台继续传送位置更新
  • 在代码中,您必须开始监视重大位置更改

  • 这是一个例子:

    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

    首次在设备上运行应用程序时,单击确定以允许应用程序在请求时在后台使用您的位置。然后双击主页按钮并将应用程序从任务切换器中滑出以将其终止。点击home,然后从屏幕底部向上滑动,打开控制中心,打开飞行模式,等待几秒钟,然后关闭飞行模式。观察应用程序上的徽章计数器增量。

    它是由 iOS 在 SLC 上推出的。

    我想补充一个问题。如果您创建两个 CLLocationManager 实例并调用 startMonitoringSignificantLocationChanges 两者,然后调用 stopMonitoringSignificantLocationChanges 在一个。即使其他 CLLocationManager 将在应用程序继续运行时继续接收 SLC 事件,当您的应用程序因任何原因退出时,它不会重新启动。

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

    开始、开始、停止、退出 - 应用程序不会在 SLC 上重新启动。

    启动、启动、停止启动、退出 - 应用程序在 SLC 上重新启动。

    关于background - iOS9 startMonitoringSignificantLocationChanges 终止时不启动应用程序?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33368607/

    24 4 0
    Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
    广告合作:1813099741@qq.com 6ren.com