forked from Rapsssito/react-native-background-actions
-
Notifications
You must be signed in to change notification settings - Fork 1
/
RNBackgroundActions.m
53 lines (45 loc) · 1.17 KB
/
RNBackgroundActions.m
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
@import UIKit;
#import "RNBackgroundActions.h"
@implementation RNBackgroundActions {
UIBackgroundTaskIdentifier bgTask;
}
RCT_EXPORT_MODULE()
- (NSArray<NSString *> *)supportedEvents
{
return @[@"expiration"];
}
- (void) _start
{
[self _stop];
bgTask = [[UIApplication sharedApplication] beginBackgroundTaskWithName:@"RNBackgroundActions" expirationHandler:^{
[self onExpiration];
[[UIApplication sharedApplication] endBackgroundTask: self->bgTask];
self->bgTask = UIBackgroundTaskInvalid;
}];
}
- (void) _stop
{
if (bgTask != UIBackgroundTaskInvalid) {
[[UIApplication sharedApplication] endBackgroundTask:bgTask];
bgTask = UIBackgroundTaskInvalid;
}
}
- (void)onExpiration
{
[self sendEventWithName:@"expiration"
body:@{}];
}
RCT_EXPORT_METHOD(start:(NSDictionary *)options
resolver:(RCTPromiseResolveBlock)resolve
rejecter:(RCTPromiseRejectBlock)reject)
{
[self _start];
resolve(nil);
}
RCT_EXPORT_METHOD(stop:(RCTPromiseResolveBlock)resolve
rejecter:(RCTPromiseRejectBlock)reject)
{
[self _stop];
resolve(nil);
}
@end