问题
my app running in the background and I want to take the html data from the web server every 60 second. (with alamofire or nsurlconnection etc..) but I could not find sample code associated with it. Is it possible to do something like this. updated every minute I want to do.
applications running in the background mode, updateServer function I want to call every 60 second
note: with background fetch methot not working every minute. func application(application: UIApplication, performFetchWithCompletionHandler completionHandler: (UIBackgroundFetchResult) -> Void) this function not calling in background mode after 3 minutes
func updateServer() {
self.requesti = request(.POST, "http://www.myserver.com/deerer.aspx"
.response { request, response, data, error in
let dataString = NSString(data: data!, encoding:NSUTF8StringEncoding)
.......
let alis = scanned as! String
let satis = scanned as! String
self.addDataBase(alis, satis: satis)
}
}
func addDataBase(alis: String, satis: String) {
let appDelegate = UIApplication.sharedApplication().delegate as! AppDelegate
let kur = NSEntityDescription.insertNewObjectForEntityForName("Kur", inManagedObjectContext: appDelegate.managedObjectContext) as! Entity
kur.alis = alis
kur.satis = satis
kur.tarih = NSDate()
appDelegate.saveContext()
let notification = UILocalNotification()
notification.alertBody = "testtesttest"
notification.fireDate = NSDate(timeIntervalSinceNow: 1)
notification.soundName = UILocalNotificationDefaultSoundName
UIApplication.sharedApplication().scheduleLocalNotification(notification)
}
回答1:
There's a good tutorial on running tasks in the background http://www.raywenderlich.com/92428/background-modes-ios-swift-tutorial
and here's a snippet that I have just tested, which seems to work nicely. This one just tries to download a webpage and display the html in a label, but it should show the important bits!
class ViewController: UIViewController
{
var bDataLoaded : Bool = false
var bTimerRunning : Bool = false
var backgroundTask: UIBackgroundTaskIdentifier = UIBackgroundTaskInvalid
var updateTimer = NSTimer()
@IBOutlet weak var lblLabel1: UILabel!
override func viewDidLoad()
{
super.viewDidLoad()
}
@IBAction func cmdButton(sender: AnyObject)
{
tryToDownload()
}
func tryToDownload()
{
if bDataLoaded
{
return
}
var html: NSString = ""
do
{
html = try NSString(contentsOfURL: NSURL(string: "http://www.myserver.com/deerer.aspx")!, encoding: NSUTF8StringEncoding)
}
catch _
{
print("Still nothing - try again")
updateTimer = NSTimer.scheduledTimerWithTimeInterval(60, target: self,
selector: "tryToDownload", userInfo: nil, repeats: true)
registerBackgroundTask()
bTimerRunning = true
}
if html != ""
{
lblLabel1.text = html as String
bDataLoaded = true
endBackgroundTask()
}
}
func registerBackgroundTask() {
backgroundTask = UIApplication.sharedApplication().beginBackgroundTaskWithExpirationHandler {
[unowned self] in
self.endBackgroundTask()
}
assert(backgroundTask != UIBackgroundTaskInvalid)
}
func endBackgroundTask() {
NSLog("Background task ended.")
UIApplication.sharedApplication().endBackgroundTask(backgroundTask)
backgroundTask = UIBackgroundTaskInvalid
}
}
来源:https://stackoverflow.com/questions/34772013/ios-call-function-in-background-mode