-->

iOS Background Fetch and completion block

2019-05-20 09:12发布

问题:

I am trying to define this method

- (void)backgroundFetchWithCompletion:(void(^)(UIBackgroundFetchResult))completionHandler;

However I am getting an error on UIBackgroundFetchResult saying a parameter list without types is only allowed, I was following this tutorial and this tutorial and that is how they define their method.

回答1:

After executing some operation at the end you must call one in list.

Objective-C

  1. completionHandler(UIBackgroundFetchResultNewData);
  2. completionHandler(UIBackgroundFetchResultFailed);
  3. completionHandler(UIBackgroundFetchResultNoData);

Swift

  1. completionHandler(.NewData)
  2. completionHandler(.Failed)
  3. completionHandler(.NoData)

Full Examples:

Objective-C

- (void)application:(UIApplication *)application performFetchWithCompletionHandler:(void (^)(UIBackgroundFetchResult))completionHandler
{
  NSLog(@"performFetchWithCompletionHandler");
  //Perform some operation
  completionHandler(UIBackgroundFetchResultNewData);
}

Swift

func application(application: UIApplication, performFetchWithCompletionHandler completionHandler: (UIBackgroundFetchResult) -> Void) {
  println("performFetchWithCompletionHandler")
  //Perform some operation
  completionHandler(.NewData)
}


回答2:

Just make sure you import UIKit.h in header file:

#import <UIKit/UIKit.h>


回答3:

[edit] This was an older answer of mine, refer to AbdullahDiaa's answer below for a better/more correct resolution.

I was just experiencing the same issue, and the solution was to add this to the file where the method with that block was defined:

#import "AppDelegate.h"