我想在应用程序处于后台模式时每 60 秒调用一次 post 方法。我的目标是存储用户位置并将其发送到我的服务器。到目前为止,出于测试目的,我使用beginBackgroundTaskWithExpirationHandler
了NSTime
每 3 秒调用一次临时打电话后,它工作正常。但问题是,这个后台任务在 10 次调用后被停止。但我希望它在一定的时间间隔内无限期地调用它(假设每 60 秒或一天一次)。
应用程序 iOS 部署目标:9.0 及其具有 swift 兼容性的 Objective C 项目。到目前为止,我已经这样做了:
- (void)applicationWillResignActive:(UIApplication *)application {
self.backgroundUpdateTask = [[UIApplication sharedApplication] beginBackgroundTaskWithExpirationHandler:^{
[self endBackgroundUpdateTask];
}];
}
- (void) endBackgroundUpdateTask
{
[[UIApplication sharedApplication] endBackgroundTask: self.backgroundUpdateTask];
self.backgroundUpdateTask = UIBackgroundTaskInvalid;
}
- (void)applicationWillEnterForeground:(UIApplication *)application {
[self endBackgroundUpdateTask];
}
- (void)applicationDidEnterBackground:(UIApplication *)application {
[NSTimer scheduledTimerWithTimeInterval:3.0 repeats:YES block:^(NSTimer * _Nonnull timer) {
[self targetMethod];
}];
}
任何人都可以帮助我如何targetMethod
定期调用它?
谢谢