-
Notifications
You must be signed in to change notification settings - Fork 19
Expand file tree
/
Copy pathMYBackgroundMonitor.m
More file actions
137 lines (102 loc) · 3.46 KB
/
Copy pathMYBackgroundMonitor.m
File metadata and controls
137 lines (102 loc) · 3.46 KB
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
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
//
// MYBackgroundMonitor.m
// MYUtilities
//
// Created by Jens Alfke on 9/24/15.
// Copyright © 2015 Jens Alfke. All rights reserved.
//
#if TARGET_OS_IPHONE
#import "MYBackgroundMonitor.h"
#import <UIKit/UIKit.h>
#import <dispatch/dispatch.h>
@implementation MYBackgroundMonitor
{
NSString* _name;
UIBackgroundTaskIdentifier _bgTask;
}
@synthesize onAppBackgrounding=_onAppBackgrounding, onAppForegrounding=_onAppForegrounding;
@synthesize onBackgroundTaskExpired=_onBackgroundTaskExpired;
static BOOL runningInAppExtension() {
return [[[[NSBundle mainBundle] bundlePath] pathExtension] isEqualToString: @"appex"];
}
static UIApplication* sharedApplication() {
return [[UIApplication class] performSelector: @selector(sharedApplication)];
}
- (instancetype) init {
self = [super init];
if (self) {
_bgTask = UIBackgroundTaskInvalid;
}
return self;
}
- (void) start {
if (runningInAppExtension())
return;
[[NSNotificationCenter defaultCenter] addObserver: self
selector: @selector(appBackgrounding:)
name: UIApplicationDidEnterBackgroundNotification
object: nil];
[[NSNotificationCenter defaultCenter] addObserver: self
selector: @selector(appForegrounding:)
name: UIApplicationWillEnterForegroundNotification
object: nil];
// Already in the background? Better start a background session now:
dispatch_async(dispatch_get_main_queue(), ^{
if (sharedApplication().applicationState == UIApplicationStateBackground)
[self appBackgrounding: nil];
});
}
- (void) stop {
if (runningInAppExtension())
return;
[[NSNotificationCenter defaultCenter] removeObserver: self];
[self endBackgroundTask];
}
- (void) dealloc {
[self stop];
}
- (BOOL) endBackgroundTask {
if (runningInAppExtension())
return NO;
@synchronized(self) {
if (_bgTask == UIBackgroundTaskInvalid)
return NO;
[sharedApplication() endBackgroundTask: _bgTask];
_bgTask = UIBackgroundTaskInvalid;
return YES;
}
}
- (BOOL) beginBackgroundTaskNamed: (NSString*)name {
if (runningInAppExtension())
return NO;
@synchronized(self) {
if (_bgTask == UIBackgroundTaskInvalid) {
_bgTask = [sharedApplication() beginBackgroundTaskWithName: name
expirationHandler: ^{
// Process ran out of background time before endBackgroundTask was called.
// NOTE: Called on the main thread
if (_bgTask != UIBackgroundTaskInvalid) {
if (_onBackgroundTaskExpired)
_onBackgroundTaskExpired();
[self endBackgroundTask];
}
}];
}
return (_bgTask != UIBackgroundTaskInvalid);
}
}
- (BOOL) hasBackgroundTask {
@synchronized(self) {
return _bgTask != UIBackgroundTaskInvalid;
}
}
- (void) appBackgrounding: (NSNotification*)n {
if (_onAppBackgrounding)
_onAppBackgrounding();
}
- (void) appForegrounding: (NSNotification*)n {
if (_onAppForegrounding)
_onAppForegrounding();
}
@end
#endif // TARGET_OS_IPHONE