#if OBJC
#import <BackgroundTasks/BackgroundTasks.h>
// Background Processing Task
//*******************************************************************************************************//
- (void) registerBackgroundProcessingTask {
@try {
// Registering the task
BOOL success = [[BGTaskScheduler sharedScheduler] registerForTaskWithIdentifier:@"com.yourcompany.processing"
usingQueue:nil
launchHandler:^(BGTask *task) {
[self handleBackgroundProcessingTask:task];
}];
[self.bi raiseUIEvent:nil event:@"registeringbackgroundprocessingtask_done::" params:@[@(NO),(@"")]];
} @catch (NSException *exception) {
[self.bi raiseUIEvent:nil event:@"registeringbackgroundprocessingtask_done::" params:@[@(NO),([exception reason])]];
}
}
- (void) scheduleBackgroundProcessingTask {
@try {
NSError *error = NULL;
// Cancel existing task (if any)
[BGTaskScheduler.sharedScheduler cancelTaskRequestWithIdentifier:@"com.yourcompany.processing"];
// New task
BGProcessingTaskRequest *request = [[BGProcessingTaskRequest alloc] initWithIdentifier:@"com.yourcompany.processing"];
// Specifying if the processing task requires a device connected to power.
request.requiresExternalPower = NO;
// Specifying if the processing task requires network connectivity.
request.requiresNetworkConnectivity = NO;
// The earliest date and time at which to run the task.
// Setting the property indicates that the background task shouldn’t start any earlier than this date.
// However, the system doesn’t guarantee launching the task at the specified date, but only that it won’t
// begin sooner.
// 900 seconds = 15 minutes
request.earliestBeginDate = [NSDate dateWithTimeIntervalSinceNow:900];
// Scheduling the task
BOOL success = [[BGTaskScheduler sharedScheduler] submitTaskRequest:request error:&error];
if (!success) {
[self.bi raiseUIEvent:nil event:@"schedulingbackgroundprocessingtask_done::" params:@[@(NO),([error localizedDescription])]];
} else {
[self.bi raiseUIEvent:nil event:@"schedulingbackgroundprocessingtask_done::" params:@[@(YES),(@"")]];
}
} @catch (NSException *exception) {
[self.bi raiseUIEvent:nil event:@"schedulingbackgroundprocessingtask_done::" params:@[@(NO),([exception reason])]];
}
}
- (void) handleBackgroundProcessingTask:(BGTask *)task {
@try {
[self scheduleBackgroundProcessingTask];
// Create an operation (or use your own logic)
NSOperationQueue *queue = [[NSOperationQueue alloc] init];
queue.maxConcurrentOperationCount = 1;
__block NSString* taskResult = @"";
__weak typeof(self) weakSelf = self;
NSBlockOperation *operation = [NSBlockOperation blockOperationWithBlock:^{
taskResult = [weakSelf _dobackgroundprocessingtask];
}];
task.expirationHandler = ^{
[operation cancel];
};
operation.completionBlock = ^{
if (operation.isCancelled) {
[self.bi raiseUIEvent:nil event:@"backgroundprocessingtask_done::" params:@[@(NO),(@"Background task expired.")]];
} else {
if ([taskResult isEqualToString:@"Done."]) {
[self.bi raiseUIEvent:nil event:@"backgroundprocessingtask_done::" params:@[@(YES),(@"")]];
} else {
[self.bi raiseUIEvent:nil event:@"backgroundprocessingtask_done::" params:@[@(NO),(taskResult)]];
}
}
[task setTaskCompletedWithSuccess:!operation.isCancelled];
};
[queue addOperation:operation];
} @catch (NSException *exception) {
[self.bi raiseUIEvent:nil event:@"backgroundprocessingtask_done::" params:@[@(NO),([exception reason])]];
[task setTaskCompletedWithSuccess:NO];
}
}
//*******************************************************************************************************//
// Background App Refresh Task
//*******************************************************************************************************//
- (void) registerBackgroundAppRefreshTask {
@try {
// Registering the task
BOOL success = [[BGTaskScheduler sharedScheduler] registerForTaskWithIdentifier:@"com.yourcompany.apprefresh"
usingQueue:nil
launchHandler:^(BGTask *task) {
[self handleBackgroundAppRefreshTask:task];
}];
[self.bi raiseUIEvent:nil event:@"registeringbackgroundapprefreshtask_done::" params:@[@(success),(@"")]];
} @catch (NSException *exception) {
[self.bi raiseUIEvent:nil event:@"registeringbackgroundapprefreshtask_done::" params:@[@(NO),([exception reason])]];
}
}
- (void) scheduleBackgroundAppRefreshTask {
@try {
NSError *error = NULL;
// Cancel existing task (if any)
[BGTaskScheduler.sharedScheduler cancelTaskRequestWithIdentifier:@"com.yourcompany.apprefresh"];
// New task
BGAppRefreshTaskRequest *request = [[BGAppRefreshTaskRequest alloc] initWithIdentifier:@"com.yourcompany.apprefresh"];
// The earliest date and time at which to run the task.
// Setting the property indicates that the background task shouldn’t start any earlier than this date.
// However, the system doesn’t guarantee launching the task at the specified date, but only that it won’t
// begin sooner.
// 900 seconds = 15 minutes
request.earliestBeginDate = [NSDate dateWithTimeIntervalSinceNow:900];
// Scheduling the task
BOOL success = [[BGTaskScheduler sharedScheduler] submitTaskRequest:request error:&error];
if (!success) {
[self.bi raiseUIEvent:nil event:@"schedulingbackgroundapprefreshtask_done::" params:@[@(NO),([error localizedDescription])]];
} else {
[self.bi raiseUIEvent:nil event:@"schedulingbackgroundapprefreshtask_done::" params:@[@(YES),(@"")]];
}
} @catch (NSException *exception) {
[self.bi raiseUIEvent:nil event:@"schedulingbackgroundapprefreshtask_done::" params:@[@(NO),([exception reason])]];
}
}
- (void) handleBackgroundAppRefreshTask:(BGTask *)task {
@try {
[self scheduleBackgroundAppRefreshTask];
// Create an operation (or use your own logic)
NSOperationQueue *queue = [[NSOperationQueue alloc] init];
queue.maxConcurrentOperationCount = 1;
__block NSString* taskResult = @"";
__weak typeof(self) weakSelf = self;
NSBlockOperation *operation = [NSBlockOperation blockOperationWithBlock:^{
taskResult = [weakSelf _dobackgroundapprefreshtask];
}];
task.expirationHandler = ^{
[operation cancel];
};
operation.completionBlock = ^{
if (operation.isCancelled) {
[self.bi raiseUIEvent:nil event:@"backgroundapprefreshtask_done::" params:@[@(NO),(@"Background task expired.")]];
} else {
if ([taskResult isEqualToString:@"Done."]) {
[self.bi raiseUIEvent:nil event:@"backgroundapprefreshtask_done::" params:@[@(YES),(@"")]];
} else {
[self.bi raiseUIEvent:nil event:@"backgroundapprefreshtask_done::" params:@[@(NO),(taskResult)]];
}
}
[task setTaskCompletedWithSuccess:!operation.isCancelled];
};
[queue addOperation:operation];
} @catch (NSException *exception) {
[self.bi raiseUIEvent:nil event:@"backgroundapprefreshtask_done::" params:@[@(NO),([exception reason])]];
[task setTaskCompletedWithSuccess:NO];
}
}
//*******************************************************************************************************//
#End If