我希望我的应用程序即使已终止也能获取位置更新我尝试使用它来注册位置更改,SignificantLocationChanges
但我似乎没有得到任何更新。
在我的AppDelegate.m
我有以下设置:
@interface AppDelegate () <RCTBridgeDelegate, CLLocationManagerDelegate>
@property (nonatomic, strong) UMModuleRegistryAdapter *moduleRegistryAdapter;
@property (nonatomic, strong) CLLocationManager* locationManager;
@property BOOL didShowLocationUpdateNotification;
@end
@implementation AppDelegate
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{
self.locationManager = [[CLLocationManager alloc] init];
[self.locationManager setDelegate:self];
/// other setup
[super application:application didFinishLaunchingWithOptions:launchOptions];
return YES;
}
- (void)locationManager:(CLLocationManager *)manager didChangeAuthorizationStatus:(CLAuthorizationStatus)status{
switch(status) {
case kCLAuthorizationStatusNotDetermined:
NSLog(@"locationManagerDidChangeAuthorization - kCLAuthorizationStatusNotDetermined");
break;
case kCLAuthorizationStatusRestricted:
NSLog(@"locationManagerDidChangeAuthorization - kCLAuthorizationStatusRestricted");
break;
case kCLAuthorizationStatusDenied:
NSLog(@"locationManagerDidChangeAuthorization - kCLAuthorizationStatusDenied");
break;
case kCLAuthorizationStatusAuthorizedAlways:
[self.locationManager setAllowsBackgroundLocationUpdates:YES];
[self.locationManager startUpdatingLocation];
[self.locationManager startMonitoringSignificantLocationChanges];
break;
case kCLAuthorizationStatusAuthorizedWhenInUse:
NSLog(@"locationManagerDidChangeAuthorization - kCLAuthorizationStatusAuthorizedWhenInUse");
break;
}
}
- (void)locationManager:(CLLocationManager *)manager didUpdateLocations:(NSArray<CLLocation *> *)locations {
NSLog(@"didUpdateLocation");
NSLog(@"Locations %@", locations);
}
我启用了以下背景模式:
我也有这些钥匙在我的info.plist
:
<key>NSLocationAlwaysAndWhenInUseUsageDescription</key>
<string>Your location is required to track your journeys. By selecting ALWAYS your journeys will record automatically.</string>
<key>NSLocationAlwaysUsageDescription</key>
<string>Your location is required to track your journeys. </string>
<key>NSLocationWhenInUseUsageDescription</key>
<string>Your location is required to track your journeys. WARNING: Journeys will not automatically record with 'When In Use' selected</string>
然而,在我终止应用程序后,即使我彻底改变了我的位置,它也永远不会再次醒来。我还在这里纠结什么?我正在使用 iOS 15 的 iPhone 11 上对其进行测试。