From 2eaf40749749279b5365200d08f9c5a5c5f21c09 Mon Sep 17 00:00:00 2001 From: Jan Vennemann Date: Thu, 7 May 2020 00:47:29 +0200 Subject: [PATCH] fix: use existing registerBackgroundService --- .../Classes/TiAppiOSBackgroundServiceProxy.h | 3 +- .../Classes/TiAppiOSBackgroundTaskProxy.swift | 51 ++++++++++++------- iphone/Classes/TiAppiOSProxy.m | 44 ++++++++-------- iphone/Resources/app.js | 2 +- .../TitaniumKit/Sources/API/TiApp.m | 1 + 5 files changed, 56 insertions(+), 45 deletions(-) diff --git a/iphone/Classes/TiAppiOSBackgroundServiceProxy.h b/iphone/Classes/TiAppiOSBackgroundServiceProxy.h index 4bec506b3ce..4a94a5d271f 100644 --- a/iphone/Classes/TiAppiOSBackgroundServiceProxy.h +++ b/iphone/Classes/TiAppiOSBackgroundServiceProxy.h @@ -7,10 +7,11 @@ #ifdef USE_TI_APPIOS +#import "Titanium-Swift.h" #import #import -@interface TiAppiOSBackgroundServiceProxy : TiProxy { +@interface TiAppiOSBackgroundServiceProxy : TiProxy { @private KrollBridge *bridge; diff --git a/iphone/Classes/TiAppiOSBackgroundTaskProxy.swift b/iphone/Classes/TiAppiOSBackgroundTaskProxy.swift index 99c95839ab0..8d5637dac44 100644 --- a/iphone/Classes/TiAppiOSBackgroundTaskProxy.swift +++ b/iphone/Classes/TiAppiOSBackgroundTaskProxy.swift @@ -9,10 +9,18 @@ import TitaniumKit import Foundation.NSOperation import BackgroundTasks -@available(iOS 13.0, *) @objc -public class TiAppiOSBackgroundTaskProxy : TiProxy { +public protocol BackgroundTaskProxy { + @objc(stop:) + func stop(args: Any) + @objc(unregister:) + func unregister(args: Any) +} + +@available(iOS 13.0, *) +@objc +public class TiAppiOSBackgroundTaskProxy : TiProxy, BackgroundTaskProxy { @objc public var type: String! @@ -23,7 +31,7 @@ public class TiAppiOSBackgroundTaskProxy : TiProxy { public var url: String! @objc - public var interval: NSNumber! + public var interval: NSNumber? @objc public var `repeat`: NSNumber { @@ -41,8 +49,8 @@ public class TiAppiOSBackgroundTaskProxy : TiProxy { private var _repeat: Bool! public override func _init(withProperties properties: [AnyHashable : Any]!) { - guard let type = properties["type"] as? String else { - self.throwException("Invalid task", subreason: "No type specified", location: CODELOCATION) + guard let type = properties["type"] as? String, (type == "refresh" || type == "process") else { + self.throwException("Invalid task", subreason: "Missing or invalid type", location: CODELOCATION) return; } guard let identifier = properties["identifier"] as? String else { @@ -53,19 +61,8 @@ public class TiAppiOSBackgroundTaskProxy : TiProxy { self.throwException("Invalid task", subreason: "No url specified", location: CODELOCATION) return; } - guard let interval = properties["interval"] as? NSNumber else { - self.throwException("Invalid task", subreason: "No interval specified", location: CODELOCATION) - return; - } - // TODO: validate type and options - if let options = properties["options"] as? [String] { - self.options = options - } - self.type = type - self.identifier = identifier - self.url = url; - self.interval = interval + super._init(withProperties: properties) } @objc @@ -83,7 +80,9 @@ public class TiAppiOSBackgroundTaskProxy : TiProxy { } request = processingRequest } - request.earliestBeginDate = Date(timeIntervalSinceNow: self.interval.doubleValue) + if let interval = self.interval { + request.earliestBeginDate = Date(timeIntervalSinceNow: interval.doubleValue) + } do { try BGTaskScheduler.shared.submit(request) @@ -91,6 +90,14 @@ public class TiAppiOSBackgroundTaskProxy : TiProxy { NSLog("Could not schedule background task: \(error)") } } + + public func stop(args: Any) { + BGTaskScheduler.shared.cancel(taskRequestWithIdentifier: self.identifier) + } + + public func unregister(args: Any) { + TiApp.sharedApp()?.unregisterBackgroundTask(self) + } } @available(iOS 13.0, *) @@ -100,14 +107,20 @@ extension TiApp { if (self.backgroundTasks == nil) { self.backgroundTasks = NSMutableDictionary() } - self.backgroundTasks.setObject(taskProxy, forKey: taskProxy.identifier as NSString); + self.backgroundTasks.setObject(taskProxy, forKey: taskProxy.identifier! as NSCopying); BGTaskScheduler.shared.register(forTaskWithIdentifier: taskProxy.identifier, using: nil) { (task) in self.handleBackgroundTask(task) } } + @objc(unregisterBackgroundTask:) + func unregisterBackgroundTask(_ taskProxy: TiAppiOSBackgroundTaskProxy) { + self.backgroundTasks.removeObject(forKey: taskProxy.identifier!); + } + func handleBackgroundTask(_ task: BGTask) { let taskProxy = self.backgroundTasks[task.identifier] as! TiAppiOSBackgroundTaskProxy + taskProxy.schedule() let url = taskProxy.url! let queue = Foundation.OperationQueue() diff --git a/iphone/Classes/TiAppiOSProxy.m b/iphone/Classes/TiAppiOSProxy.m index 2d1a560da58..35692e0091c 100644 --- a/iphone/Classes/TiAppiOSProxy.m +++ b/iphone/Classes/TiAppiOSProxy.m @@ -392,7 +392,7 @@ - (TiAppiOSUserDefaultsProxy *)createUserDefaults:(id)args #pragma mark - Background Services & Tasks -- (TiAppiOSBackgroundServiceProxy *)registerBackgroundService:(id)args +- (TiProxy *)registerBackgroundService:(id)args { NSDictionary *a = nil; ENSURE_ARG_AT_INDEX(a, args, 0, NSDictionary) @@ -403,35 +403,31 @@ - (TiAppiOSBackgroundServiceProxy *)registerBackgroundService:(id)args return nil; } - if (backgroundServices == nil) { - backgroundServices = [[NSMutableDictionary alloc] init]; - } + NSString *identifier = [a objectForKey:@"identifier"]; + if ([TiUtils isIOSVersionOrGreater:@"13.0"] && identifier != nil) { + if (self.backgroundTasks == nil) { + self.backgroundTasks = [NSMutableDictionary dictionary]; + } - TiAppiOSBackgroundServiceProxy *proxy = [backgroundServices objectForKey:urlString]; + TiAppiOSBackgroundTaskProxy *task = [[[TiAppiOSBackgroundTaskProxy alloc] _initWithPageContext:self.executionContext args:args] autorelease]; + [TiApp.app registerBackgroundTask:task]; - if (proxy == nil) { - proxy = [[[TiAppiOSBackgroundServiceProxy alloc] _initWithPageContext:[self executionContext] args:args] autorelease]; - [backgroundServices setValue:proxy forKey:urlString]; - } + return task; + } else { + if (backgroundServices == nil) { + backgroundServices = [[NSMutableDictionary alloc] init]; + } - [[TiApp app] registerBackgroundService:proxy]; - return proxy; -} + TiAppiOSBackgroundServiceProxy *proxy = [backgroundServices objectForKey:urlString]; -- (TiAppiOSBackgroundTaskProxy *)registerBackgroundTask:(id)args API_AVAILABLE(ios(13)) -{ - NSDictionary *options = nil; - ENSURE_ARG_AT_INDEX(options, args, 0, NSDictionary); - NSString *identifier = options[@"identifier"]; + if (proxy == nil) { + proxy = [[[TiAppiOSBackgroundServiceProxy alloc] _initWithPageContext:[self executionContext] args:args] autorelease]; + [backgroundServices setValue:proxy forKey:urlString]; + } - if (self.backgroundTasks == nil) { - self.backgroundTasks = [NSMutableDictionary dictionary]; + [[TiApp app] registerBackgroundService:proxy]; + return proxy; } - - TiAppiOSBackgroundTaskProxy *task = [[[TiAppiOSBackgroundTaskProxy alloc] _initWithPageContext:self.executionContext args:args] autorelease]; - [TiApp.app registerBackgroundTask:task]; - - return task; } - (void)registerUserNotificationSettings:(id)args diff --git a/iphone/Resources/app.js b/iphone/Resources/app.js index ba56df52c13..6890ddf6975 100644 --- a/iphone/Resources/app.js +++ b/iphone/Resources/app.js @@ -6,7 +6,7 @@ * to trigger a log that is displayed in the Xcode console. */ -Ti.App.iOS.registerBackgroundTask({ +Ti.App.iOS.registerBackgroundService({ type: 'refresh', identifier: 'com.appc.test', interval: 15 * 60, diff --git a/iphone/TitaniumKit/TitaniumKit/Sources/API/TiApp.m b/iphone/TitaniumKit/TitaniumKit/Sources/API/TiApp.m index fdc0f0bc9cd..2cda6a7ee71 100644 --- a/iphone/TitaniumKit/TitaniumKit/Sources/API/TiApp.m +++ b/iphone/TitaniumKit/TitaniumKit/Sources/API/TiApp.m @@ -1164,6 +1164,7 @@ - (void)dealloc RELEASE_TO_NIL(remoteNotification); RELEASE_TO_NIL(splashScreenView); RELEASE_TO_NIL(backgroundServices); + RELEASE_TO_NIL(_backgroundTasks); RELEASE_TO_NIL(localNotification); RELEASE_TO_NIL(uploadTaskResponses); RELEASE_TO_NIL(queuedBootEvents);