Advertisement
IGERBIT

IOS Background Task

Sep 25th, 2023 (edited)
1,532
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Objective C 1.01 KB | Software | 0 0
  1. #import <UIKit/UIKit.h>
  2.  
  3. @interface Background : NSObject {
  4.     UIBackgroundTaskIdentifier bgTask;
  5. }
  6. @end
  7.  
  8. @implementation Background
  9.  
  10. -(void)startTask {
  11.     [self endTask];
  12.     if (bgTask == UIBackgroundTaskInvalid) {
  13.         bgTask = [[UIApplication sharedApplication] beginBackgroundTaskWithExpirationHandler:^{
  14.             [[UIApplication sharedApplication] endBackgroundTask:bgTask];
  15.             bgTask = UIBackgroundTaskInvalid;
  16.         }];
  17.     }
  18. }
  19.  
  20. -(void)endTask {
  21.     if (bgTask != UIBackgroundTaskInvalid) {
  22.         [[UIApplication sharedApplication] endBackgroundTask:bgTask];
  23.         bgTask = UIBackgroundTaskInvalid;
  24.     }
  25.     [UIApplication sharedApplication].idleTimerDisabled = NO;
  26. }
  27.  
  28. static Background *bg = NULL;
  29.  
  30. extern "C" {
  31.    
  32.     void LaunchBackgroundTaskIOS() {
  33.         if (bg == NULL)
  34.             bg = [[Background alloc] init];
  35.         [bg startTask];
  36.     }
  37.    
  38.     void StopBackgroundTaskIOS() {
  39.         if (bg != NULL)
  40.             [bg endTask];
  41.     }
  42. }
  43.  
  44. @end
  45.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement