Browse Source

Add: BackgroundTasks

marinofaggiana 4 years ago
parent
commit
8ae449d5f2

+ 4 - 0
Nextcloud.xcodeproj/project.pbxproj

@@ -265,6 +265,7 @@
 		F7AE00F8230E81CB007ACF8A /* NCBrowserWeb.swift in Sources */ = {isa = PBXBuildFile; fileRef = F7AE00F7230E81CB007ACF8A /* NCBrowserWeb.swift */; };
 		F7AE00FA230E81EB007ACF8A /* NCBrowserWeb.storyboard in Resources */ = {isa = PBXBuildFile; fileRef = F7AE00F9230E81EB007ACF8A /* NCBrowserWeb.storyboard */; };
 		F7AF7633246BEDFE00B86E3C /* TOPasscodeViewController.framework in Frameworks */ = {isa = PBXBuildFile; fileRef = F7AF7632246BEDFE00B86E3C /* TOPasscodeViewController.framework */; };
+		F7B1076D25D3CF2800E72DE2 /* BackgroundTasks.framework in Frameworks */ = {isa = PBXBuildFile; fileRef = F7B1076C25D3CF2800E72DE2 /* BackgroundTasks.framework */; };
 		F7B174C822FAC0A8000B7579 /* AppDelegate.m in Sources */ = {isa = PBXBuildFile; fileRef = F7B174C722FAC0A8000B7579 /* AppDelegate.m */; };
 		F7B2DEF01F976854007CF4D2 /* NYMnemonic.m in Sources */ = {isa = PBXBuildFile; fileRef = F7B2DEEF1F976785007CF4D2 /* NYMnemonic.m */; };
 		F7B2DEF11F976859007CF4D2 /* english.txt in Resources */ = {isa = PBXBuildFile; fileRef = F7B2DEED1F976785007CF4D2 /* english.txt */; };
@@ -623,6 +624,7 @@
 		F7AE00F7230E81CB007ACF8A /* NCBrowserWeb.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = NCBrowserWeb.swift; sourceTree = "<group>"; };
 		F7AE00F9230E81EB007ACF8A /* NCBrowserWeb.storyboard */ = {isa = PBXFileReference; lastKnownFileType = file.storyboard; path = NCBrowserWeb.storyboard; sourceTree = "<group>"; };
 		F7AF7632246BEDFE00B86E3C /* TOPasscodeViewController.framework */ = {isa = PBXFileReference; lastKnownFileType = wrapper.framework; name = TOPasscodeViewController.framework; path = Carthage/Build/iOS/TOPasscodeViewController.framework; sourceTree = "<group>"; };
+		F7B1076C25D3CF2800E72DE2 /* BackgroundTasks.framework */ = {isa = PBXFileReference; lastKnownFileType = wrapper.framework; name = BackgroundTasks.framework; path = System/Library/Frameworks/BackgroundTasks.framework; sourceTree = SDKROOT; };
 		F7B174C722FAC0A8000B7579 /* AppDelegate.m */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.objc; path = AppDelegate.m; sourceTree = "<group>"; };
 		F7B1A7761EBB3C8000BFB6D1 /* de */ = {isa = PBXFileReference; lastKnownFileType = text.plist.strings; name = de; path = de.lproj/Localizable.strings; sourceTree = "<group>"; };
 		F7B2DEED1F976785007CF4D2 /* english.txt */ = {isa = PBXFileReference; lastKnownFileType = text; path = english.txt; sourceTree = "<group>"; };
@@ -766,6 +768,7 @@
 				F7DBD82C23E46A4700ECB7C6 /* MarkdownKit.framework in Frameworks */,
 				F75E57BD25BF0EC1002B72C2 /* SVGKit in Frameworks */,
 				F74AFCE922E8B025003DE61F /* FSCalendar.framework in Frameworks */,
+				F7B1076D25D3CF2800E72DE2 /* BackgroundTasks.framework in Frameworks */,
 				F7D2C773246470CA008513AE /* XLForm.framework in Frameworks */,
 				F782A57D25123694007BBABD /* RealmSwift in Frameworks */,
 				F7176DBC256672640017E83C /* FirebaseCoreDiagnostics.framework in Frameworks */,
@@ -1479,6 +1482,7 @@
 		F7FC7D541DC1F93700BB2C6A /* Frameworks */ = {
 			isa = PBXGroup;
 			children = (
+				F7B1076C25D3CF2800E72DE2 /* BackgroundTasks.framework */,
 				F7F35B592578FB63003F5589 /* CollaboraOnlineWebViewKeyboardManager.framework */,
 				F7176DDA256672D90017E83C /* libsqlite3.tbd */,
 				F7176DB9256672640017E83C /* FIRAnalyticsConnector.framework */,

+ 1 - 0
iOSClient/AppDelegate.h

@@ -24,6 +24,7 @@
 #import <Foundation/Foundation.h>
 #import <UserNotifications/UserNotifications.h>
 #import <AVKit/AVKit.h>
+#import <BackgroundTasks/BackgroundTasks.h>
 #import <LocalAuthentication/LocalAuthentication.h>
 #import <TOPasscodeViewController/TOPasscodeViewController.h>
 #import "CCLogin.h"

+ 54 - 0
iOSClient/AppDelegate.m

@@ -163,6 +163,11 @@
     // Auto upload
     self.networkingAutoUpload = [NCNetworkingAutoUpload new];
     
+    if (@available(iOS 13.0, *)) {
+        NSLog(@"configureProcessingTask");
+        [self configureProcessingTask];
+    }
+    
     return YES;
 }
 
@@ -241,6 +246,11 @@
     [[NSNotificationCenter defaultCenter] postNotificationOnMainThreadName:NCBrandGlobal.shared.notificationCenterApplicationDidEnterBackground object:nil];
     
     [self passcodeWithAutomaticallyPromptForBiometricValidation:false];
+    
+    if (@available(iOS 13.0, *)) {
+        NSLog(@"scheduleProcessingTask");
+        [self scheduleProcessingTask];
+    }
 }
 
 //
@@ -502,6 +512,50 @@
     }];
 }
 
+#pragma mark Background Task
+
+-(void)configureProcessingTask
+{
+    if (@available(iOS 13.0, *)) {
+        [[BGTaskScheduler sharedScheduler] registerForTaskWithIdentifier:NCBrandGlobal.shared.backgroudTask usingQueue:nil launchHandler:^(BGTask *task) {
+            [self scheduleLocalNotifications];
+            [self handleProcessingTask:task];
+        }];
+    } else {
+        // No fallback
+    }
+}
+
+-(void)scheduleLocalNotifications
+{
+    //do things
+}
+
+-(void)handleProcessingTask:(BGTask *)task API_AVAILABLE(ios(13.0))
+{
+    //do things with task
+}
+
+-(void)scheduleProcessingTask
+{
+    if (@available(iOS 13.0, *)) {
+        NSError *error = NULL;
+        // cancel existing task (if any)
+        [BGTaskScheduler.sharedScheduler cancelTaskRequestWithIdentifier:NCBrandGlobal.shared.backgroudTask];
+        // new task
+        BGProcessingTaskRequest *request = [[BGProcessingTaskRequest alloc] initWithIdentifier:NCBrandGlobal.shared.backgroudTask];
+        request.requiresNetworkConnectivity = YES;
+        request.earliestBeginDate = [NSDate dateWithTimeIntervalSinceNow:5];
+        BOOL success = [[BGTaskScheduler sharedScheduler] submitTaskRequest:request error:&error];
+        if (!success) {
+            // Errorcodes https://stackoverflow.com/a/58224050/872051
+            NSLog(@"Background task failed to submit request: %@", error);
+        } else {
+            NSLog(@"Background task success submit request %@", request);
+        }
+    }
+}
+
 #pragma mark Fetch
 
 - (void)application:(UIApplication *)application performFetchWithCompletionHandler:(void (^)(UIBackgroundFetchResult))completionHandler

+ 2 - 1
iOSClient/Brand/NCBrand.swift

@@ -263,9 +263,10 @@ class NCBrandColor: NSObject {
     @objc let appScan                               = "Library/Application Support/Scan"
     @objc let directoryProviderStorage              = "File Provider Storage"
 
-    // Service Key Share
+    // Service
     @objc let serviceShareKeyChain                  = "Crypto Cloud"
     @objc let metadataKeyedUnarchiver               = "it.twsweb.nextcloud.metadata"
+    @objc let backgroudTask                         = "com.nextcloud.backgroudTask"
 
     // Nextcloud version
     @objc let nextcloudVersion12: Int               =  12