首页 文章

ios在后台模式下调用功能

提问于
浏览
-1

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

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

注意:背景提取methot每分钟都不工作 . func应用程序(应用程序:UIApplication,performFetchWithCompletionHandler completionHandler:(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)
}

1 回答

  • 0

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

    这是我刚刚测试过的片段,看起来效果很好 . 这个只是试图下载一个网页并在标签中显示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
        }
    }
    

相关问题