简体   繁体   English

iOS 后台访问令牌刷新

[英]iOS 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).到期时间当前设置为 30 分钟(服务器端)并且此值不会更改,其想法是每 29 分钟我将调用一个方法来刷新访问令牌以使其存活 1 年半小时(意味着访问令牌保持活动状态将近 2 小时)。 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.我已经读过后台任务最多需要 5 秒才能完成,然后如果需要更多时间来使用 UIKit,尽管这只会允许再花几分钟来完成所需的任务。

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!感谢您的任何帮助!

In AppDelegate.swift do this.在 AppDelegate.swift 中执行此操作。

func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplicationLaunchOptionsKey: Any]?) -> Bool {
    // Override point for customization after application launch.
    Timer.scheduledTimer(timeInterval: (60.0 * 29.0), target: self, selector: #selector(self.doThisEvery29Min), userInfo: nil, repeats: true)
    return true
}

@objc func doThisEvery29Min () {
    if Constant.uToken != nil {
        ServiceManager.sharedInstance.RefreshTokens() {
            (result) in
            switch result {
            case .Success(let responce):
                // update token
            case .FailureDueToService(let error):
                print(error)
                break
            case .Failure(let error):
                print(error)
                break
            }
        }
    }
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM