2015-11-20 59 views
2

我正在開發一個基於位置的應用程序,應該始終提取用戶位置。我使用標準位置服務。但問題是,在後臺保持空閒一段時間之後,即使移動到其他位置後,應用也不會獲取座標。根據蘋果文檔,當新的位置到達時,應用程序應該自動喚醒,但這不會發生在這裏。我正在分享代碼並使用它來獲取plist的位置和截圖。基於標準位置的iOS應用程序在暫停後不醒來

class SALocation: NSObject,CLLocationManagerDelegate 
{ 
    static let sharedInstance : SALocation = SALocation() 

    var locationManager : CLLocationManager! 
    var location : CLLocation! 
    var address : String! 
    var latitude : NSString? 
    var longitude : NSString? 
    var isAdderssLoaded : Bool = false 
    var locdictionary : NSMutableDictionary = NSMutableDictionary() 

    func startLocationManager() 
    { 
     if self.locationManager == nil 
     { 
      self.locationManager = CLLocationManager() 

      if CLLocationManager.locationServicesEnabled(){ 
       print("location service enabled") 
      } 
      self.locationManager.delegate = self 
      self.locationManager.requestWhenInUseAuthorization() 
      self.locationManager.distanceFilter = kCLDistanceFilterNone 
      self.locationManager.desiredAccuracy = kCLLocationAccuracyBest 

      if (Float (UIDevice.currentDevice().systemVersion) >= 9) { 
       if #available(iOS 9.0, *) { 
        self.locationManager.allowsBackgroundLocationUpdates = true 
       } else { 
        // Fallback on earlier versions 
       }; 

      } 

      self.locationManager.startUpdatingLocation() 
      //self.locationManager.stopMonitoringSignificantLocationChanges() 
     } 
     else 
     { 
      self.locationManager.startUpdatingLocation() 
     } 
    } 

    // MARK: CLLocationManagerDelegate 
    func locationManager(manager: CLLocationManager, didFailWithError error: NSError) 
    { 
     UIAlertView(title:"Alert", message:error.description, delegate: nil, cancelButtonTitle:nil, otherButtonTitles:"Ok").show() 
    } 
    func locationManager(manager: CLLocationManager, didUpdateLocations locations: [CLLocation]) 
    { 
     if locations.count > 0 
     { 
      self.location = locations[0] 
      /* storing date and location to plist 

      */ 

      let datenow = NSDate() 
      let dateformatternow = NSDateFormatter() 
      dateformatternow.dateFormat = "yyyyMMdd HH:mm:ss" 
      let timenow:NSString = dateformatternow.stringFromDate(datenow) 
      let documetsdirectorypath = NSSearchPathForDirectoriesInDomains(NSSearchPathDirectory.DocumentDirectory, NSSearchPathDomainMask.UserDomainMask, true).last 
      latitude = NSString(format: "%f",self.location.coordinate.latitude) 
      longitude = NSString (format: "%f",self.location.coordinate.longitude) 
      let latlong : NSString = NSString(format:"%@~%@",latitude!,longitude!) 
      NSUserDefaults.standardUserDefaults().setObject(latlong, forKey: "latlong") 
      let aFilePath = NSString(format: "%@/location.plist",documetsdirectorypath!) 
      locdictionary.setObject(latlong, forKey: timenow as String) 
      locdictionary.writeToFile(aFilePath as String, atomically: true) 
      ///////////// ||storing date and location to plist code ends here||\\\\\\ 





      // self.getAddressFromLocation(locations[0]) 
//   if (NSUserDefaults.standardUserDefaults().objectForKey(SettingAppRefresh) != nil) 
//   { 
//    if (NSUserDefaults.standardUserDefaults().objectForKey(SettingAppRefresh) as! NSString).isEqualToString(FalseString) 
//    { 
//     // self.locationManager.stopUpdatingLocation() 
//    } 
//   } 
     } 
    } 

    } 

我在這裏做的只是獲取位置並將其寫入plist文件。這工作在前臺,背景等罰款。但是,當我把應用程序空閒20分鐘,位置並不牽強,即使我移動到其他位置的應用程序被暫停

enter image description here

功能選項卡看起來像這樣enter image description here

回答

2

啓動位置,背景則必須從以下路徑

點擊你的名字啓動後臺服務 - >點擊您的應用程序名稱(目標) - >轉到功能 - >找到後臺模式 - >啓用位置更新模式

enter image description here

我不知道你是啓動還是不因爲你不把任何截圖這一點。

並且檢查你的用戶是否在settings.refer下面的鏈接中啓動了後臺刷新。

Background App Refresh checking, enabling and disabling programatically for whole device and for each particular application in iOS 7

更新::使用下面的鏈接 對於後臺位置更新(目標C)

http://www.creativeworkline.com/2014/12/core-location-manager-ios-8-fetching-location-background/

+0

我已經這樣做了,唯一的問題是設備保持空閒一段時間會使應用程序暫停並停止提取位置。即使我們移動到某個其他位置,應用程序仍然處於暫停狀態如何喚醒應用程序?即時通訊計劃要做的是顯着變化的位置服務... –

+0

@SurajKThomas請看我更新的答案。 –

1

嗯,我不知道你是如何獲得位置更新 - 重要的位置變化作爲例子,以及你如何退出背景。 我建議檢查您的應用程序是否真正處於後臺模式 - UIApplication.sharedApplication()。applicationState,因爲它可以被終止。 我也建議您查看Apple的Execution States for Apps。 - 特別是針對您實施長時間運行任務部分的可能用例。 rayywenderlich.com上還有一個名爲Background modes的好教程。

1

請使用

self.locationManager.requestAlwaysAuthorization()

,不要忘記更新Info.plist以定義NSLocationAlwaysUsageDescription項。

相關問題