我是新来的后台任务。我有一个小工作,我正在获取推文,如果我的应用程序处于后台模式,那么它也应该获取推文,但我不知道如何。如何使用Swift 3使用后台任务?
我在的appdelegate didFinishLaunchOption方法使用简单的定时器。当我关闭应用程序时,它不起作用。我是新手,所以请提出任何建议。下面是我的代码:
Timer.scheduledTimer(timeInterval: 10, target: self, selector: #selector(getTweets), userInfo: nil, repeats: true).
func getTweets() {
let locationName = Helper.sharedInstance.userDefault.value(forKey: ModelKey.currentLocation) as? String
let accessToken = Helper.sharedInstance.userDefault.value(forKey: ModelKey.twitterAccessToken) as? String
if (locationName == "Bengaluru" && nil != accessToken) || (locationName == "Bangalore" && nil != accessToken){
tweetModel.getTweets(accessToken: accessToken!, city: ModelKey.blrcitytraffic, cityName: "Bengaluru")
}
}
文本到语音也有,但是当我关闭应用程序然后停止发言。如果我不使用应用程序,那么它也可以获取推文和文本到语音应该使用背景模式工作。这工作多久了?
相关:https://developer.apple.com/library/content/documentation/ iPhone /概念/ iPhoneOSProgrammingGuide/BackgroundExecution/BackgroundExecution.html –