【问题标题】:ios call function in background modeios后台调用函数
【发布时间】:2016-04-18 17:47:54
【问题描述】:

我的应用程序在后台运行,我想每 60 秒从 Web 服务器获取 html 数据。 (使用 alamofire 或 nsurlconnection 等)但我找不到与之相关的示例代码。有没有可能做这样的事情。每分钟更新一次。

以后台模式运行的应用程序,我想每 60 秒调用一次 updateServer 函数

注意:后台获取方法并非每分钟都有效。 func 应用程序(应用程序:UIApplication,performFetchWithCompletionHandler 完成处理程序:(UIBackgroundFetchResult)-> Void) 此函数在 3 分钟后未在后台模式下调用

    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)
}

【问题讨论】:

标签: ios swift alamofire uiapplication


【解决方案1】:

后台运行任务有很好的教程http://www.raywenderlich.com/92428/background-modes-ios-swift-tutorial

这是我刚刚测试过的一个 sn-p,它似乎工作得很好。这个只是尝试下载一个网页并在标签中显示 html,但它应该显示重要的部分!

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
    }
}

【讨论】:

  • 这个函数在 3 分钟后没有在后台调用
  • 通读链接的文章。它解释了如何设置后台模式,在 3 分钟后启用下载。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2011-10-15
  • 2017-09-08
  • 2011-07-23
  • 1970-01-01
相关资源
最近更新 更多