hi, i am attempting to create a background task for my kivy-ios app. The code i have so for is this:
from pyobjus import autoclass, protocol, objc_str, selector, objc_b
from pyobjus.dylib_manager import make_dylib, load_dylib
from pyobjus.dylib_manager import load_framework, INCLUDE
load_framework('/System/Library/Frameworks/BackgroundTasks.framework')
NSString = autoclass('NSString')
NSObject = autoclass('NSObject')
uniqueIdent = NSString.alloc().initWithUTF8String_('org.kivy.fleetdriver.trackdriver')
NSDate = autoclass('NSDate').alloc().init()
#print("date:",NSDate)
#BGTask = autoclass('BGTask').alloc()
#print("my bgtask instance",dir(BGTask))
#NSObject = autoclass('NSObject')
#print("uniqe indentifier:",uniqueIdent)
#BGTaskScheduler = NSObject
BGTaskScheduler = autoclass('BGTaskScheduler').alloc()
#BGTaskScheduler.init()
print("my background scheduler instance:", dir(BGTaskScheduler))
BGAppRefreshTaskRequest = autoclass("BGAppRefreshTaskRequest").alloc()
#print("my BGAppRefrfesh instance:",dir(BGAppRefreshTaskRequest))
#print("setting identifier")
#request(uniqueIdent)
request = BGAppRefreshTaskRequest
request.identifier = uniqueIdent
request.init()
request.earliestBeginDate = NSDate
try:
BGTaskScheduler.shared.submit(request)
except Exception as e:
print(e)
How ever when i run the app in Xcode Iphone simulator it complains
that the BGTaskScheduler has no attribute named shared is also not in the list when I dir(BGTaskScheduler)). Check the output here:
my background scheduler instance: ['.cxx_destruct', 'CAMLType', 'CAMLTypeForKey_', 'CAMLTypeSupportedForKey_', 'CA_addValue_multipliedBy_', 'CA_archivingValueForKey_', 'CA_copyNumericValue_', 'CA_copyRenderValue', 'CA_distanceToValue_', 'CA_interpolateValue_byFraction_', 'CA_interpolateValues___interpolator_', 'CA_prepareRenderValue', 'CA_roundToIntegerFromValue_', 'CA_validateValue_forKey_', 'CKAssignToContainerWithID_', 'CKDescription', 'CKDescriptionPropertiesWithPublic_private_shouldExpand_', 'CKExpandedDescription', 'CKHashedDescription', 'CKPropertiesDescription', 'CKPropertiesDescriptionStringFromProperties_', 'CKSingleLineDescription', 'CKStatusReport', 'CalClassName', 'NSRepresentation', 'RBSIsXPCObject', 'SCN_setupDisplayLinkWithQueue_screen_', '_CKDescriptionWithExpansion_', '_ICSStringWithOptions_', '_ICSStringWithOptions_appendingToString_', '_NSItemProviderTypeCoercion_coercedUIImageValueFromNSDataValue_error_', '_NSItemProviderTypeCoercion_coercedUIImageValueFromNSURLValue_error_', '___tryRetain_OA', '__autorelease_OA', '__autorotationSanityCheckObjectFromSource_selector_', '__class__', '__cls_storage', '__copy_properties__', '__dealloc_zombie', '__delattr__', '__dict__', '__dir__', '__doc__', '__eq__', '__format__', '__ge__', '__getattribute__', '__getter___lock', '__getter___permittedIdentifiers', '__getter___registrations', '__getter___runningTasksMap', '__gt__', '__hash__', '__im_afterDelay_', '__im_afterDelay_modes_', '__im_getInvocation_', '__im_onDetachedThread', '__im_onMainThread', '__im_onMainThreadIfNecessary', '__im_onThread_', '__im_onThread_immediateForMatchingThread_', '__init__', '__init_subclass__', '__isKindOfUIView', '__ivarDescriptionForClass_', '__le__', '__lt__', '__methodDescriptionForClass_', '__module__', '__ne__', '__new__', '__objcclass__', '__propertyDescriptionForClass_', '__pyx_vtable__', '__reduce__', '__reduce_ex__', '__release_OA', '__repr__', '__retain_OA', '__setattr__', '__setstate__', '__sizeof__', '__str__', '__subclasshook__', '__weakref__', '_accessibilityFinalize', '_accessibilityGetContextID', '_addObserver_forProperty_options_context_', '_allowsDirectEncoding', '_applyTraitStorageRecordsForTraitCollection_', '_cfTypeID', '_changeValueForKey_key_key_usingBlock_', '_changeValueForKey_usingBlock_', '_changeValueForKeys_count_maybeOldValuesDict_maybeNewValuesDict_usingBlock_', '_conformsToProtocolNamed_', '_connectInterfaceBuilderEventConnection_', '_copyDescription', '_cplCopyProperties_fromOtherObject_withCopyBlock_', '_destroyObserverList', '_didChangeValuesForKeys_', '_didEndKeyValueObserving', '_handleAppLaunch', '_implicitObservationInfo', '_init', '_intents_compareValue_relation_', '_intents_displayImageForLanguage_', '_intents_displayImageWithLocalizer_', '_intents_enumerateObjectsOfClass_withBlock_', '_intents_indexingRepresentation', '_intents_isValidKey_', '_intents_localizedCopyForLanguage_', '_intents_localizedCopyWithLocalizer_', '_intents_readableDescriptionForLanguage_', '_intents_readableDescriptionForLanguage_withMetadata_', '_intents_readableDescriptionWithLocalizer_', '_intents_readableDescriptionWithLocalizer_metadata_', '_intents_readableSubtitleForLanguage_', '_intents_readableSubtitleForLanguage_withMetadata_', '_intents_readableSubtitleWithLocalizer_', '_intents_readableSubtitleWithLocalizer_metadata_', '_intents_readableTitleForLanguage_', '_intents_readableTitleForLanguage_withMetadata_', '_intents_readableTitleWithLocalizer_', '_intents_readableTitleWithLocalizer_metadata_', '_intents_setterForPropertyWithName_', '_internalAccessibilityAttributedHint', '_internalAccessibilityAttributedLabel', '_internalAccessibilityAttributedUserInputLabels', '_internalAccessibilityAttributedValue', '_internalSetAccessibilityAttributedHint_', '_internalSetAccessibilityAttributedLabel_', '_internalSetAccessibilityAttributedUserInputLabels_', '_internalSetAccessibilityAttributedValue_', '_isDeallocating', '_isGestureType_', '_isKVOA', '_isMKClusterAnnotation', '_isRunningTaskOfClass_', '_isToManyChangeInformation', '_ivarDescription', '_keyboardActivate', '_keyboardActivateEventDown_', '_localClassNameForClass', '_lock', '_mapkit_implementationOfSelector_isFromSubclassOfClass_', '_mapkit_isInternalAnnotation', '_mapkit_isInternalAnnotationView', '_methodDescription', '_mf_checkToAllowExclusiveLocksWithLock_', '_mf_checkToAllowLock_', '_mf_checkToAllowOrderingWithLock_', '_mf_checkToAllowStrictProgressionWithLock_', '_mf_dumpLockCallStacks_ordering_', '_mf_lockOrderingForType_', '_mf_ntsCheckToAllowLock_', '_mf_ntsIsLocked', '_na_addNotificationObserverProxy_', '_na_removeNotificationObserverProxy_', '_notifyObserversForKeyPath_change_', '_notifyObserversOfChangeFromValuesForKeys_toValuesForKeys_', '_observerStorage', '_observerStorageOfSize_', '_overrideUseFastBlockObservers', '_pendingChangeNotificationsArrayForKey_create_', '_permittedIdentifiers', '_pl_prettyDescription', '_pl_prettyDescriptionWithIndent_', '_propertyDescription', '_queueForRegistration_', '_receiveBox_', '_registrations', '_removeObserver_forProperty_', '_runTask_registration_', '_runningTasks', '_runningTasksMap', '_setLock_', '_setPermittedIdentifiers_', '_setRegistrations_', '_setRunningTasksMap_', '_setTraitStorageList_', '_shortMethodDescription', '_simulateExpirationForTaskWithIdentifier_', '_simulateLaunchForTaskWithIdentifier_', '_traitStorageList', '_tryRetain', '_ui_descriptionBuilder', '_uikit_applyValueFromTraitStorage_forKeyPath_', '_uikit_valueForTraitCollection_', '_uikit_variesByTraitCollections', '_unsafe_registerForTaskWithIdentifier_usingQueue_launchHandler_', '_unsafe_registrationForIdentifier_', '_unsafe_submitTaskRequest_error_', '_web_description', '_webkit_invokeOnMainThread', '_willBeginKeyValueObserving', '_willChangeValuesForKeys_', 'accessibilityActivate', 'accessibilityActivationPoint', 'accessibilityAssistiveTechnologyFocusedIdentifiers', 'accessibilityAttributedHint', 'accessibilityAttributedLabel', 'accessibilityAttributedUserInputLabels', 'accessibilityAttributedValue', 'accessibilityContainer', 'accessibilityContainerType', 'accessibilityCustomActions', 'accessibilityCustomRotors', 'accessibilityDecrement', 'accessibilityDragSourceDescriptors', 'accessibilityDropPointDescriptors', 'accessibilityElementAtIndex_', 'accessibilityElementCount', 'accessibilityElementDidBecomeFocused', 'accessibilityElementDidLoseFocus', 'accessibilityElementIsFocused', 'accessibilityElements', 'accessibilityElementsHidden', 'accessibilityFrame', 'accessibilityHeaderElements', 'accessibilityHint', 'accessibilityIdentification', 'accessibilityIdentifier', 'accessibilityIncrement', 'accessibilityLabel', 'accessibilityLanguage', 'accessibilityLocalizedStringKey', 'accessibilityNavigationStyle', 'accessibilityPath', 'accessibilityPerformEscape', 'accessibilityPerformMagicTap', 'accessibilityRespondsToUserInteraction', 'accessibilityScroll_', 'accessibilitySetIdentification_', 'accessibilityTextualContext', 'accessibilityTraits', 'accessibilityUserInputLabels', 'accessibilityValue', 'accessibilityViewIsModal', 'addChainedObservers_', 'addObservationTransformer_', 'addObserverBlock_', 'addObserver_', 'addObserver_forKeyPath_options_context_', 'addObserver_forObservableKeyPath_', 'allowSafePerformSelector', 'allowsWeakReference', 'ams_generateDescriptionWithSubObjects_', 'associatedObjectForKey_', 'autoContentAccessingProxy', 'autorelease', 'awakeAfterUsingCoder_', 'awakeFromNib', 'boolValueSafe', 'boolValueSafe_', 'bs_encoded', 'bs_isPlistableType', 'bs_secureEncoded', 'cancelAllTaskRequests', 'cancelTaskRequestWithIdentifier_', 'ck_bindInStatement_atIndex_', 'classForArchiver', 'classForCoder', 'classForKeyedArchiver', 'classForPortCoder', 'className', 'cn_updateDictionaryForKey_withChanges_', 'conformsToProtocol_', 'copy', 'cplCopyPropertiesFromObject_withCopyBlock_', 'cplCopyProperties_fromObject_withCopyBlock_', 'cplDecodePropertiesFromCoder_', 'cplDeepCopy', 'cplEncodePropertiesWithCoder_', 'cplFullDescription', 'cplHash', 'cplIsEqual_', 'cplIsEqual_withEqualityBlock_', 'cplProperties_areEqualToPropertiesOf_diffTracker_withEqualityBlock_', 'cplProperties_areEqualToPropertiesOf_withEqualityBlock_', 'cplSpecialHash', 'cplSpecialIsEqual_', 'currentContextCanExecuteSelector_', 'da_addNullRunLoopSourceAndPerformSelector_withObject_afterDelay_inModes_', 'da_performSelectorThatDoesntAffectRetainCount_withObject_', 'dcaxRespondsToSelector_fromExtrasProtocol_', 'dcaxValueForKey_', 'dealloc', 'debugDescription', 'debugHierarchyAdditionalGroupingIDs', 'debugHierarchyObjectsInGroupWithID_outOptions_', 'debugHierarchyPropertyDescriptions', 'debugHierarchyValueForPropertyWithName_', 'defaultAccessibilityTraits', 'description', 'descriptionAtIndent_', 'dictionaryWithValuesForKeys_', 'didChangeValueForKey_', 'didChangeValueForKey_withSetMutation_usingObjects_', 'didChange_valuesAtIndexes_forKey_', 'disallowSafePerformSelector', 'doesNotRecognizeSelector_', 'doubleValueSafe', 'doubleValueSafe_', 'ef_observeKeyPath_options_autoCancelToken_usingBlock_', 'ef_observeKeyPath_options_usingBlock_', 'ef_onScheduler_', 'enc', 'encodeWithCAMLWriter_', 'finalize', 'finishObserving', 'forwardInvocation_', 'forwardingTargetForSelector_', 'fromMainThreadPostNotificationName_object_userInfo_', 'fromNotifySafeThreadPerformSelector_withObject_', 'fromNotifySafeThreadPostNotificationName_object_userInfo_', 'getPendingTaskRequestsWithCompletionHandler_', 'get_address', 'handleInvalidExecutionContextForSelector_requirement_', 'hash', 'if_setValueIfNonNil_forKey_', 'if_setValueIfYES_forKey_', 'implementsSelector_', 'indexOfAccessibilityElement_', 'init', 'initFromPQLResultSet_error_', 'initWithCPLArchiver_', 'int64ValueSafe', 'int64ValueSafe_', 'isAccessibilityElement', 'isAccessibilityElementByDefault', 'isElementAccessibilityExposedToInterfaceBuilder', 'isEqual_', 'isFault', 'isKindOfClass_', 'isMemberOfClass_', 'isNSArray__', 'isNSCFConstantString__', 'isNSData__', 'isNSDate__', 'isNSDictionary__', 'isNSNumber__', 'isNSObject__', 'isNSOrderedSet__', 'isNSSet__', 'isNSString__', 'isNSTimeZone__', 'isNSValue__', 'isNull', 'isProxy', 'methodDescriptionForSelector_', 'methodForSelector_', 'methodSignatureForSelector_', 'mf_exclusiveLocks', 'mf_lock', 'mf_lockOrdering', 'mf_lockWithPriority', 'mf_objectWithHighest_', 'mf_strictLockOrdering', 'mf_tryLock', 'mf_tryLockWithPriority', 'mf_unlock', 'multicaster_queueForSelector_', 'mutableArrayValueForKeyPath_', 'mutableArrayValueForKey_', 'mutableCopy', 'mutableOrderedSetValueForKeyPath_', 'mutableOrderedSetValueForKey_', 'mutableSetValueForKeyPath_', 'mutableSetValueForKey_', 'na_addNotificationBlockObserverForName_object_queue_usingBlock_', 'na_addNotificationBlockObserverForObject_keyPath_options_usingBlock_', 'na_associatedObjectForDescriptor_', 'na_removeNotificationBlockObserver_', 'na_setAssociatedObject_assocatedObjectDescriptor_', 'observationInfo', 'observeValueForKeyPath_ofObject_change_context_', 'oclass', 'okToNotifyFromThisThread', 'pep_afterDelay_', 'pep_getInvocation_', 'pep_onDetachedThread', 'pep_onMainThread', 'pep_onMainThreadIfNecessary', 'pep_onOperationQueue_', 'pep_onOperationQueue_priority_', 'pep_onThread_', 'pep_onThread_immediateForMatchingThread_', 'performBlockOnMainThreadSynchronously_', 'performSelectorInBackground_withObject_', 'performSelectorOnMainThread_withObject_waitUntilDone_', 'performSelectorOnMainThread_withObject_waitUntilDone_modes_', 'performSelector_', 'performSelector_object_afterDelay_', 'performSelector_onThread_withObject_waitUntilDone_', 'performSelector_onThread_withObject_waitUntilDone_modes_', 'performSelector_withObject_', 'performSelector_withObject_afterDelay_', 'performSelector_withObject_afterDelay_ignoreMenuTracking_', 'performSelector_withObject_afterDelay_inModes_', 'performSelector_withObject_withObject_', 'pkaxRespondsToSelector_fromExtrasProtocol_', 'pkaxValueForKey_', 'pl_briefDescription', 'plistArchiveWithCPLArchiver_', 'postNotificationWithDescription_', 'prepareForInterfaceBuilder', 'receiveObservedError_', 'receiveObservedValue_', 'registerForTaskWithIdentifier_usingQueue_launchHandler_', 'release', 'releaseOnMainThread', 'removeObservation_', 'removeObservation_forObservableKeyPath_', 'removeObserver_forKeyPath_', 'removeObserver_forKeyPath_context_', 'replacementObjectForArchiver_', 'replacementObjectForCoder_', 'replacementObjectForKeyedArchiver_', 'replacementObjectForPortCoder_', 'respondsToSelector_', 'retain', 'retainCount', 'retainWeakReference', 'self', 'setAccessibilityActivationPoint_', 'setAccessibilityAttributedHint_', 'setAccessibilityAttributedLabel_', 'setAccessibilityAttributedUserInputLabels_', 'setAccessibilityAttributedValue_', 'setAccessibilityContainerType_', 'setAccessibilityContainer_', 'setAccessibilityCustomActions_', 'setAccessibilityCustomRotors_', 'setAccessibilityDragSourceDescriptors_', 'setAccessibilityDropPointDescriptors_', 'setAccessibilityElementsHidden_', 'setAccessibilityElements_', 'setAccessibilityFrame_', 'setAccessibilityHeaderElements_', 'setAccessibilityHint_', 'setAccessibilityIdentifier_', 'setAccessibilityLabel_', 'setAccessibilityLanguage_', 'setAccessibilityNavigationStyle_', 'setAccessibilityPath_', 'setAccessibilityRespondsToUserInteraction_', 'setAccessibilityTextualContext_', 'setAccessibilityTraits_', 'setAccessibilityUserInputLabels_', 'setAccessibilityValue_', 'setAccessibilityViewIsModal_', 'setAssociatedObject_forKey_', 'setIsAccessibilityElement_', 'setNilValueForKey_', 'setObservationInfo_', 'setObservation_forObservingKeyPath_', 'setShouldGroupAccessibilityChildren_', 'setValue_forKeyPath_', 'setValue_forKey_', 'setValue_forUndefinedKey_', 'setValuesForKeysWithDictionary_', 'shouldGroupAccessibilityChildren', 'storedAccessibilityActivationPoint', 'storedAccessibilityContainerType', 'storedAccessibilityElementsHidden', 'storedAccessibilityFrame', 'storedAccessibilityNavigationStyle', 'storedAccessibilityRespondsToUserInteraction', 'storedAccessibilityTraits', 'storedAccessibilityViewIsModal', 'storedClassNameForCPLArchiver_', 'storedIsAccessibilityElement', 'storedShouldGroupAccessibilityChildren', 'stringValueSafe', 'stringValueSafe_', 'submitTaskRequest_error_', 'superclass', 'supportsBSXPCSecureCoding', 'supportsRBSXPCSecureCoding', 'toPBCodable', 'uiPresentationManager', 'un_safeBoolValue', 'upcase_first_letter', 'utf8ValueSafe', 'utf8ValueSafe_', 'validateValue_forKeyPath_error_', 'validateValue_forKey_error_', 'valueForKeyPath_', 'valueForKey_', 'valueForUndefinedKey_', 'wfName', 'wfType', 'willChangeValueForKey_', 'willChangeValueForKey_withSetMutation_usingObjects_', 'willChange_valuesAtIndexes_forKey_', 'zone']
'BGTaskScheduler' object has no attribute 'shared'
If I do BGTaskScheduler.init() than Xcode says:
*** Assertion failure in -[BGTaskScheduler init], BGTaskScheduler.m:112
2023-03-28 18:01:12.227855-0500 fleetdriver[19847:564250] *** Terminating app due to uncaught exception 'NSInternalInconsistencyException', reason: 'Do not directly instantiate; use the shared scheduler instead'
*** First throw call stack:
(
0 CoreFoundation 0x00007fff2043a126 __exceptionPreprocess + 242
1 libobjc.A.dylib 0x00007fff20177f78 objc_exception_throw + 48
2 CoreFoundation 0x00007fff20439f4f +[NSException raise:format:] + 0
3 Foundation 0x00007fff207881ca -[NSAssertionHandler handleFailureInMethod:object:file:lineNumber:description:] + 191
4 BackgroundTasks 0x00007fff4a63633f -[BGTaskScheduler init] + 101
5 fleetdriver 0x0000000101650eec ffi_call_unix64 + 76
6 ??? 0x00006000031a16a0 0x0 + 105553168307872
)
libc++abi.dylib: terminating with uncaught exception of type NSException
*** Terminating app due to uncaught exception 'NSInternalInconsistencyException', reason: 'Do not directly instantiate; use the shared scheduler instead'
terminating with uncaught exception of type NSException
CoreSimulator 857.14 - Device: iPhone 8 (FE3220DE-9123-46AF-B736-D1B36B3149ED) - Runtime: iOS 14.1 (18A8394) - DeviceType: iPhone 8
can somebody point me into the right direction or share some examples.Thank you