iOs设备动作无法在后台模式和暂停模式下工作

时间:2016-06-21 21:20:49

标签: ios swift xcode7

我正在开发一个定位应用程序,主要的想法是尽可能地传达手机的位置,但试图节省手机能量,我正在尝试做的是用'CMMotionActivityManager'检测设备运动并尝试激活gps或不像以下代码:

// Start Device Activity
    func startDeviceActivity() {
        NSLog("startDeviceActivity method")
        DebugNotification.sharedInstance().debugNotification("startDeviceActivity method")
        if(CMMotionActivityManager.isActivityAvailable()){
            NSLog("CMMotionActivityManager is Available")
            DebugNotification.sharedInstance().debugNotification("CMMotionActivityManager is Available")
            self.activityManager.startActivityUpdatesToQueue(NSOperationQueue.mainQueue(), withHandler: { (data: CMMotionActivity?) in
                dispatch_async(dispatch_get_main_queue(), { () -> Void in
                    NSLog("startActivityUpdatesToQueue")
                    DebugNotification.sharedInstance().debugNotification("startActivityUpdatesToQueue")
                    self.monitorDeviceActivity = true
                    if data!.confidence != CMMotionActivityConfidence.Low {
                        if data!.automotive || data!.cycling || data!.running || data!.walking {
                            // Here we cover walking, running, automotive and cycling
                            if !self.deviceMoving {
                                NSLog("Device MOVING: Automotive or Cycling or Running or Automotive")
                                // Debug Notification
                                DebugNotification.sharedInstance().debugNotification("Device MOVING: Automotive or Cycling or Running or Automotive")
                                self.deviceMoving = true
                                self.turnOnGPS()
                                self.timer.invalidate()
                            }
                        }else{
                            // Here we cover unknown y stationary
                            if self.deviceMoving {
                                NSLog("Device on Stationary Mode")
                                // Debug Notification
                                DebugNotification.sharedInstance().debugNotification("Device on Stationary Mode")
                                self.deviceMoving = false
                                self.turnOffGPS()
                                self.timer = NSTimer.scheduledTimerWithTimeInterval(self.timerSeconds, target: self, selector: #selector(self.sendOnePosition), userInfo: nil, repeats: true)
                            }
                        }
                    }else{
                        NSLog("Confidence LOW")
                        DebugNotification.sharedInstance().debugNotification("Confidence LOW")
                    }
                })
            })
        }
    }

但我的主要问题是这些代码在前台模式下完美运行,后台和挂起模式不起作用,此代码也在AppDelegate文件中调用的函数内:

func applicationWillResignActive(application: UIApplication) {
     self.locationManager.startDeviceActivity()
}

func applicationDidEnterBackground(application: UIApplication) {
     self.locationManager.startDeviceActivity()
}

func applicationDidBecomeActive( application: UIApplication) {
     self.locationManager.startDeviceActivity()
}

如何启用设备动作过程?

非常感谢!

0 个答案:

没有答案