我正在尝试在应用程序终止时处理react-native应用程序中的位置更新。经过几天未能让react-native-background-geolocation或CLLocationManager始终如一地工作,我现在正在尝试使用intuit/LocationManager窗格,但我的应用程序没有重新启动以响应位置更新(或者它可能只是不记录它们?)
这是Intuit处理基于位置的发布的示例:
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{
// If you start monitoring significant location changes and your app is subsequently terminated, the system automatically relaunches the app into the background if a new event arrives.
// Upon relaunch, you must still subscribe to significant location changes to continue receiving location events.
if ([launchOptions objectForKey:UIApplicationLaunchOptionsLocationKey]) {
INTULocationManager *locMgr = [INTULocationManager sharedInstance];
[locMgr subscribeToSignificantLocationChangesWithBlock:^(CLLocation *currentLocation, INTULocationAccuracy achievedAccuracy, INTULocationStatus status) {
// This block will be executed with the details of the significant location change that triggered the background app launch,
// and will continue to execute for any future significant location change events as well (unless canceled).
}];
}
return YES;
}
这是我的代码:
- (BOOL) application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
NSLog(@"--- Initializing application");
INTULocationManager *locMgr = [INTULocationManager sharedInstance];
[locMgr subscribeToSignificantLocationChangesWithBlock:^(CLLocation *currentLocation, INTULocationAccuracy achievedAccuracy, INTULocationStatus status) {
// This block will be executed with the details of the significant location change that triggered the background app launch,
// and will continue to execute for any future significant location change events as well (unless canceled).
NSLog(@"--- location update");
}];
_app = application;
id<RCTBridgeDelegate> moduleInitialiser = self;
RCTBridge *bridge = [[RCTBridge alloc] initWithDelegate:moduleInitialiser launchOptions:launchOptions];
RCTRootView *rootView = [[RCTRootView alloc]
initWithBridge:bridge
moduleName:@"GoNote"
initialProperties:nil
];
rootView.backgroundColor = [[UIColor alloc] initWithRed:1.0f green:1.0f blue:1.0f alpha:1];
self.window = [[UIWindow alloc] initWithFrame:[UIScreen mainScreen].bounds];
UIViewController *rootViewController = [UIViewController new];
rootViewController.view = rootView;
self.window.rootViewController = rootViewController;
[self.window makeKeyAndVisible];
[FIRApp configure];
[[UNUserNotificationCenter currentNotificationCenter] setDelegate:self];
return YES;
}
为了测试这个,我在模拟器中打开应用程序,等待位置更新(应用程序记录“位置更新”),然后关闭应用程序(双击命令shift-h并轻扫应用程序)。然后我等待模拟器的调试位置设置为“高速公路驱动器”。
根据我的理解,应用程序应该在后台启动,我应该看到日志“正在初始化应用程序”和“位置更新”,但这种情况永远不会发生。
我在info.plist
文件中有这些条目:
<key>NSLocationAlwaysAndWhenInUseUsageDescription</key>
<string>CHANGEME: NSLocationAlwaysAndWhenInUseUsageDescription</string>
<key>NSLocationAlwaysUsageDescription</key>
<string>CHANGEME: NSLocationAlwaysUsageDescription</string>
<key>NSLocationWhenInUseUsageDescription</key>
<string>CHANGEME: NSLocationWhenInUseUsageDescription</string>
<key>NSMotionUsageDescription</key>
<string>CHANGEME: NSMotionUsageDescription</string>
<key>UIBackgroundModes</key>
<array>
<string>fetch</string>
<string>remote-notification</string>
<string>location</string>
</array>
为什么我的应用处理位置没有更新?
答案 0 :(得分:0)
iOS有几种选择如何在后台正确运行应用程序。 iOS启用了这些方法。我们将使用您的应用程序将使用的那个以及我们需要的那个。我们可以在UIBackgroundModes数组中看到的所有服务:
如果应用程序位于前台,我们可以简单地停止监视并在间隔后再次启动它,例如使用NSTimer。在这种情况下,我们必须考虑应用程序的生命周期。应用程序在后台运行并在空闲停止工作期间运行。
iOS中的本身我们无法更改系统更新用户位置的时间间隔。如果有GPS信号,IOS会每秒定期更新位置。
应用程序每5分钟更新一次背景位置,执行以下操作:
参加你的项目&#34;目标&#34; - &GT;能力 - &gt;背景模式 - &gt;选择位置更新
我的最后一个程序在后台使用NSTimer ,使用 UIApplication:beginBackgroundTaskWithExpirationHandler:。当应用程序在后台运行时,此计时器会定期触发。
现在,当您的应用在后台时,让位置正常工作,并将坐标发送到网络服务,或者每隔5分钟对它们执行任何操作,就像在下面的代码中一样。
<强> AppDelegate.m 强>
//update location
-(void)locationManager:(CLLocationManager *)manager didUpdateToLocation:(CLLocation *)newLocation fromLocation:(CLLocation *)oldLocation
{
NSLog(@"Location: %f, %f", newLocation.coordinates.longtitude, newLocation.coordinates.latitude);
}
//run background task
-(voud)runBackgroundTask: (int) time
{
//check if application is in background mode
if ([UIApplication sharedApplication].applicationState == UIApplicationStateBackground)
{
//create UIBackgroundTaskIdentifier and create tackground task, which starts after time
__block UIBackgroundTaskIdentifier bgTask = [app beginBackgroundTaskWithExpirationHandler:^{
[app endBackgroundTask:bgTask];
bgTask = UIBackgroundTaskInvalid;
}];
dispatch_async(dispatch_get_global_queue (DISPATCH_QUEUE_PRIORITY_HIGH, 0), ^{
NSTimer* t = [NSTimer scheduledTimerWithTimeInterval:time target:self selector:@selector(startTrackingBg) userInfo:nil repeats:NO];
[[NSRunLoop currentRunLoop] addTimer:t forMode:NSDefaultRunLoopMode];
[[NSRunLoop currentRunLoop] run];
});
}
}
//starts when application switchs into backghround
- (void)applicationDidEnterBackground:(UIApplication *)application
{
//check if application status is in background
if ([UIApplication sharedApplication].applicationState == UIApplicationStateBackground)
{
NSLog(@"start backgroun tracking from appdelegate");
//start updating location with location manager
[locationManager startUpdatingLocation];
}
//change locationManager status after time
[self runBackgroundTask:20];
}
//starts with background task
-(void)startTrackingBg{
//write background time remaining
NSLog(@"backgroundTimeRemaining: %.0f", [[UIApplication sharedApplication] backgroundTimeRemaining]);
//set default time
int time = 60;
//if locationManager is ON
if (locationStarted == TRUE ) {
//stop update location
[locationManager stopUpdatingLocation];
locationStarted = FALSE;
}else{
//start updating location
[locationManager startUpdatingLocation];
locationStarted = TRUE;
//ime how long the application will update your location
time = 5;
}
[self runBackgroundTask:time];
}
//application switchs back from background
- (void)applicationWillEnterForeground:(UIApplication *)application
{
locationStarted = FALSE;
//stop updating
[locationManager stopUpdatingLocation];
}