Background Access Token Refresh

I am looking to extend the expiry time of an access token by refreshing the token in the background. The expiry time is currently set to 30 minutes (server side) and this value is not to be changed, the idea is that every 29 minutes I will call a method that refreshes the access token to keep it alive for a further 1 and a half hours (meaning the access token stays alive for almost 2 hours). I have read that background tasks should take up to 5 second to complete then if more time is required to use UIKit, although this will only allow a further few minutes to complete the tasks needed.


Are there any special circumstances that will allow me to do this in background, or is there a better way to do this? Thank you for any help!

Replies

the idea is that every 29 minutes I will call a method that refreshes the access token to keep it alive

It’s unlikely you’ll be able to achieve this goal. iOS has many different ways to run in the background, but none of them allow you to get executed at consistent intervals like this.

The closest I can think of is the

earliestBeginDate
feature of
NSURLSession
, possibly combined with
-URLSession:task:willBeginDelayedRequest:completionHandler:
. There’s a couple of problems with this approach:
  • earliestBeginDate
    means exactly what it says. The task won’t necessarily start at that time, it just won’t start before that time.
  • NSURLSession
    has a resume rate limiter to prevent you from abusing features like this to run indefinitely in the background. See NSURLSession’s Resume Rate Limiter.

Share and Enjoy

Quinn “The Eskimo!”
Apple Developer Relations, Developer Technical Support, Core OS/Hardware

let myEmail = "eskimo" + "1" + "@apple.com"