如何在 iOS 8 中应用程序处于后台状态时连续执行任务

How to Execute a task continuously when application is in Background state in iOS 8

我编写了以下代码,用于在应用程序处于后台状态时执行任务,它在 iOS 7 中工作正常,但在 iOS 8 中不工作。 当应用程序处于后台状态时,有人可以给我在 iOS 8 中连续执行任务的解决方案。

- (void)applicationDidEnterBackground:(UIApplication *)application 
{
   UIApplication *app = [UIApplication sharedApplication];
   UIBackgroundTaskIdentifier bgTask;
   bgTask = [app beginBackgroundTaskWithExpirationHandler:^{
        [app endBackgroundTask:bgTask];
   }];
}

无法在后台连续执行任务(少数情况除外)。

Most apps can move to the extended state easily enough but there are also legitimate reasons for apps to continue running in the background. The techniques offered by iOS fall into three categories:

  1. Apps that start a short task in the foreground can ask for time to finish that task when the app moves to the background.
  2. Apps that initiate downloads in the foreground can hand off management of those downloads to the system, thereby allowing the app to be suspended or terminated while the download continues.
  3. Apps that need to run in the background to support specific types of tasks can declare their support for one or more background execution modes.

For tasks that require more execution time to implement, you must request specific permissions to run them in the background without their being suspended. In iOS, only specific app types are allowed to run in the background:

  1. Apps that play audible content to the user while in the background, such as a music player app
  2. Apps that record audio content while in the background
  3. Apps that keep users informed of their location at all times, such as a navigation app
  4. Apps that support Voice over Internet Protocol (VoIP)
  5. Apps that need to download and process new content regularly
  6. Apps that receive regular updates from external accessories

Apps that implement these services must declare the services they support and use system frameworks to implement the relevant aspects of those services. Declaring the services lets the system know which services you use, but in some cases it is the system frameworks that actually prevent your application from being suspended.

是的,我在实施 ibeacon 时实施了后台条件。委托设置在 接口

@interface HomeMainVC ()<ESTBeaconManagerDelegate>{

如果用户通过以下功能进入或存在信标区域,我能够发送本地通知。所以必须有一些方法可以将用户位置发送到。

//Beacon manager did enter region
- (void)beaconManager:(ESTBeaconManager *)manager didEnterRegion:(ESTBeaconRegion *)region
{}

//Beacon Manager did exit the region
- (void)beaconManager:(ESTBeaconManager *)manager didExitRegion:(ESTBeaconRegion *)region
{}

您使用了以下代码,但它仅在应用程序发送到后台模式时被调用一次,因此您没有用到它。如果你只想在后台模式下做一些事情,那么它对你很有用。

- (void)applicationDidEnterBackground:(UIApplication *)application 
{
   UIApplication *app = [UIApplication sharedApplication];
   UIBackgroundTaskIdentifier bgTask;
   bgTask = [app beginBackgroundTaskWithExpirationHandler:^{
        [app endBackgroundTask:bgTask];
   }];
}