即使应用程序处于后台或锁定模式直到完成,如何执行任务

问题描述 投票:0回答:1

下面的代码我已经习惯在后台模式下写一些任务。但是这个任务在结束任务后只执行了3分钟。所以我想执行特定的任务,直到它完成。

func registerBackgroundTask() {
    backgroundTask = UIApplication.shared.beginBackgroundTask { [weak self] in
        self?.endBackgroundTask()   
    }
    assert(backgroundTask != UIBackgroundTaskInvalid)
}

func endBackgroundTask() {
    print("Background task ended.")
    AppConstants.sharedInstance.scheduleLocalNotification(message: "Background task ended.", title: "status!")
    UIApplication.shared.endBackgroundTask(backgroundTask)
    backgroundTask = UIBackgroundTaskInvalid
}

self.registerBackgroundTask()
self.perform(#selector(self.backgroundTimer(challengeId:)), with: "\(tag)", afterDelay:600)
ios swift uiapplication performselector
1个回答
0
投票

正如在这个doc中发现的那样,只允许预先定义(由Apple)任务(在doc的表3-1中)无限期地运行。在完成任务之前,您无法执行任务。但是如果你的目标是jail破坏设备,你可以通过编写守护进程来实现。这似乎不是你的情况。所以,你没有运气。

© www.soinside.com 2019 - 2024. All rights reserved.