Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Enabled support for starting multiple tasks and closing a specific ta… #1

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions src/ios/BackgroundTask.h
Original file line number Diff line number Diff line change
Expand Up @@ -9,5 +9,6 @@
@interface BackgroundTask : CDVPlugin

- (void) start:(CDVInvokedUrlCommand*)command;
- (void) stop:(CDVInvokedUrlCommand*)command;

@end
22 changes: 14 additions & 8 deletions src/ios/BackgroundTask.m
Original file line number Diff line number Diff line change
Expand Up @@ -7,26 +7,32 @@
#import "Cordova/CDVViewController.h"
#import "BackgroundTask.h"

static UIBackgroundTaskIdentifier backgroundTaskId;

@implementation BackgroundTask

+ (void) initialize
{
backgroundTaskId = UIBackgroundTaskInvalid;
}

- (void) start:(CDVInvokedUrlCommand*)command;
{
backgroundTaskId = [[UIApplication sharedApplication] beginBackgroundTaskWithExpirationHandler:^{
__block UIBackgroundTaskIdentifier backgroundTaskId = [[UIApplication sharedApplication] beginBackgroundTaskWithExpirationHandler:^{
[[UIApplication sharedApplication] endBackgroundTask:backgroundTaskId];
backgroundTaskId = UIBackgroundTaskInvalid;
}];

[self.commandDelegate runInBackground:^{
CDVPluginResult* pluginResult = [CDVPluginResult resultWithStatus:CDVCommandStatus_OK];
CDVPluginResult* pluginResult = [CDVPluginResult
resultWithStatus:CDVCommandStatus_OK
messageAsInt:(int)backgroundTaskId];
[self.commandDelegate sendPluginResult:pluginResult callbackId:command.callbackId];
}];
}

- (void) stop:(CDVInvokedUrlCommand*)command;
{
if ([command.arguments count] > 0) {
int backgroundTaskId = [[command.arguments objectAtIndex:0] intValue];
[[UIApplication sharedApplication] endBackgroundTask:(UIBackgroundTaskIdentifier)backgroundTaskId];
} else {
NSLog(@"Called BackgroundTask.stop without TaskId");
}
}

@end
4 changes: 4 additions & 0 deletions www/backgroundtask.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,4 +9,8 @@ BackgroundTask.prototype.start = function (task) {
exec(task, null, "BackgroundTask", "start", []);
};

BackgroundTask.prototype.stop = function (taskId) {
exec(null, null, "BackgroundTask", "stop", [taskId]);
};

module.exports = new BackgroundTask();