his has been driving me potty for two days now, and I've eventually got to the bottom of it - but no idea where to go now.
App structure
- Single view app, nothing in the view controller. App delegate has only boilerplate code, aside from implementing
handleEventsForBackgroundURLSession
- Framework that extends
,URLSessionDownloadDelegate
andURLSessionDelegate
. The methods implemented essentially just log messages.URLSessionTaskDelegate
- Notification Service Extension. This handles push notifications which we want to do things in response to.
- Targeting iOS 11, using a real device. I can't test on the simulator as we're using push notifications
What should happen
- Push message received by the notification service extension. This creates a background
with aURLSession
,sharedContainerIdentifier
and starts off a download.sessionSendsLaunchEvents = true
- The download completes, and, depending on the length, either returns in the
method of the Framework, ordidFinishDownloadingTo
on the app delegate. This behaviour is well documented here : https://forums.developer.apple.com/message/225659#225659handleEventsForBackgroundURLSession
Background
I already have an app that has been using push notifications for a while, and want to implement this into. I created a test app with the above structure to prove it works, and it does. I then spent two days removing my hair while I was completely unable to get
handleEventsForBackgroundURLSession
to fire for longer running downloads.I therefore have two projects, with essentially the same structure and code. It is now to the point where I've basically copied the working one into the non-working one. However, one works, and one does not.
Where it goes wrong
In a fit of rage and after exhausting every other sensible option, I decided to simply rename the bundle identifier on the working one, and refactor everything into that. And all of a sudden
handleEventsForBackgroundURLSession
is no-longer called.The bundle is the bad guy
Yes - literally switching the bundle identifier between the two apps makes one work, and one not.
The logging
So in the console, we can see what is happening. When it's working, the
nsurlsessiond
process that is responsible for background downloads sends a request to open the app when it completes in order to fire the delegate. This simply doesn't happen in the non working instance.The problem
The bundle identifier is the thing that ties everything together in the app store. Unless I want a whole new app (I don't) I'm stuck with the "broken" bundle ID and
handleEventsForBackgroundURLSession
never being called, which *****.Has anyone ever seen anything like this in the past, and have ideas as to the cause and possible fix?