我是新來的後臺任務。我有一個小工作,我正在獲取推文,如果我的應用程序處於後臺模式,那麼它也應該獲取推文,但我不知道如何。如何使用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 –