React本机应用程序永远不会重新启动以处理iOS中的位置更新
我正在尝试在应用程序终止时处理react本机应用程序中的位置更新。在连续几天未能使或CLLocationManager保持一致工作后,我现在尝试使用pod,但我的应用程序没有重新启动以响应位置更新(或者可能只是没有记录它们?) 这是Intuit处理基于位置的启动的示例:React本机应用程序永远不会重新启动以处理iOS中的位置更新,ios,objective-c,react-native,Ios,Objective C,React Native,我正在尝试在应用程序终止时处理react本机应用程序中的位置更新。在连续几天未能使或CLLocationManager保持一致工作后,我现在尝试使用pod,但我的应用程序没有重新启动以响应位置更新(或者可能只是没有记录它们?) 这是Intuit处理基于位置的启动的示例: - (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
- (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;
}
为什么我的应用程序不能处理位置更新?iOS有几个选项可以选择如何在后台正确运行应用程序。这些方法是通过iOS启用的。我们将使用您的应用程序将要使用并且我们需要的。我们可以在UIBackgroundModes阵列中看到的所有服务: 请点击此链接:- 如果应用程序位于前台,我们可以简单地停止监视,并在间隔后再次启动它,例如使用NSTimer。在这种情况下,我们必须考虑应用程序的生命周期。应用程序在后台运行,空闲时停止工作 在iOS中,本机无法更改系统更新用户位置的间隔。如果有GPS信号,IOS会每秒定期更新位置。 应用程序每5分钟更新一次后台位置执行以下操作: 转到您的项目“目标”->功能->后台模式->选择位置更新 我的最后一个过程是在后台使用NSTimer使用UIApplication:beginBackgroundTaskWithExpirationHandler:。当应用程序在后台运行时,此计时器会定期触发 现在,当你的应用程序在后台时,让定位工作起来,每5分钟将坐标发送给web服务或使用坐标执行任何操作,如下面的代码所示。 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];
}
iOS有几个选项可以选择如何在后台正确运行应用程序。这些方法是通过iOS启用的。我们将使用您的应用程序将要使用并且我们需要的。我们可以在UIBackgroundModes阵列中看到的所有服务: 请点击此链接:- 如果应用程序位于前台,我们可以简单地停止监视,并在间隔后再次启动它,例如使用NSTimer。在这种情况下,我们必须考虑应用程序的生命周期。应用程序在后台运行,空闲时停止工作 在iOS中,本机无法更改系统更新用户位置的间隔。如果有GPS信号,IOS会每秒定期更新位置。 应用程序每5分钟更新一次后台位置执行以下操作: 转到您的项目“目标”->功能->后台模式->选择位置更新 我的最后一个过程是在后台使用NSTimer使用UIApplication:beginBackgroundTaskWithExpirationHandler:。当应用程序在后台运行时,此计时器会定期触发 现在,当你的应用程序在后台时,让定位工作起来,每5分钟将坐标发送给web服务或使用坐标执行任何操作,如下面的代码所示。 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];
}
为什么这么复杂?为什么必须在didEnterBackground生命周期方法中检查应用程序是否在后台?当应用程序位于前台时,是否会调用
ApplicationIdentinterBackground
?为什么在ApplicationIdentinterBackground
中调用startUpdatingLocation
,然后在startTrackingBg
20秒后再次调用它?为什么在开始跟踪bg
中停止位置更新?当你提到“背景”时,是否包括应用程序终止的时间?为什么这么复杂?为什么必须在didEnterBackground生命周期方法中检查应用程序是否在后台?当应用程序位于前台时,是否会调用ApplicationIdentinterBackground
?为什么在ApplicationIdentinterBackground
中调用startUpdatingLocation
,然后在startTrackingBg
20秒后再次调用它?为什么在开始跟踪bg
中停止位置更新?当您提到“后台”时,是否包括应用程序终止的时间?您是否能够使其正常工作?我把时间花在完全相同的场景中。你能让它工作吗?我把时间花在完全相同的场景中。