iOS Background tasks compile issue

I wanted to add a background task for iOS13, to replace a setMinimumBackgroundFetchInterval for earlier iOS versions since Fire kindly pointed out it was obsolete :wink:

So, I added the BackgroundTasks reference and started with the BGTaskScheduler.shared.register as per BGTaskScheduler | Apple Developer Documentation but Fire says there is no such static member (.shared) and certainly there’s no code completion so am I missing something or is it an issue?

Secondly, I need to support from iOS9 onwards, so have my deployment target set to this, but obviously it doesn’t know about background tasks, and whilst I know I can use

if available(“iOS 13.0”)

for the code, if I try and run it on a simulator earlier than iOS13, then it hangs on startup with

dyld: Library not loaded: /System/Library/Frameworks/BackgroundTasks.framework/BackgroundTasks

Finally, for development testing, is there a way we can do something like this in Fire Starting and Terminating Tasks During Development | Apple Developer Documentation ?

It looks like that page is showing Swift code snippets even when ObjC is selected as language, so you see the “swiftified” (I call em “messed up beyond recognition”) names of things. :(.

if you scroll down further, it shows the actual ObjC names, what you want is probably

Getting the Shared Task Scheduler

sharedScheduler

The shared background task scheduler instance.

Hmm, curious. that framework should be weekly linked. @ck, do you remember ow that worked? iirc that should be automatic, no? (update: yeah our docs agree that this should happen automatically: Deployment Targets)

Assuming e is an LLDB debugger command, you can run that on the Fore debug console as well, if you prefix it with lldb, eg:

lldb e -l objc -- (void)[[BGTaskScheduler sharedScheduler] _simulateLaunchForTaskWithIdentifier:@"TASK_IDENTIFIER"]

—marc

Yes it should. Any chance of a testcase that shows this? (I’m not very familiar with this stuff)

Ah, I see (and I notice a lot Apple seem to ignore ObjC a lot in their docs). That is right, and I see the issue with CC.

If I type

if available("iOS 13.0") then begin
    BGTaskScheduler.

Nothing comes up, but if I remove the first line then CC does indeed work, and sure enough there is the sharedScheduler.registerForTaskWithIdentifier that I was looking for.

It definitely doesn’t run on simulator with 12.2 unless I remove the reference.

Wow, I didn’t realise I could interact with the debug console like that. All this time !

Sure. I’ll build something now.

Here you go. Just try it on a 12.2 simulator, and it doesn’t run. Note I didn’t add any code (apart from the info.plist stuff that’s needed), all I did was add the backgroundtasks reference.Archive.zip (118.2 KB)

Thanks, logged as bugs://83680

Just to make sure it isn’t a simulator thing, I just found an iPad with 12.4 on and it fails on that as well.

1 Like

In vNext, including todays build, you can add

<Reference Include="BackgroundTasks">
      <Weak>True</Weak>
    </Reference>

to force weak-linking. I’ll see if we can automatically detect this, longer term.

Curious,. you happen to have a quick test case for that handy that reproduces this? (else I can try create one myself)

Just use the one above, then paste that into AppDelegate (didFinishLaunchingWithOptions)

Thanks, logged as bugs://83684

Reproduced. thanx!

1 Like

bugs://83684 got closed with status fixed.

1 Like

2 posts were split to a new topic: iOS: Merge Task Fails