2015-12-30 15 views
5

Ich versuche, Standort-Updates auch in allen Zuständen zu erhalten, selbst wenn die App beendet wird/beendet wird. Ich habe den Hintergrund-Abruf in xcode aktiviert und den folgenden Code implementiert (verwendeter Verweis "Capture location in all states app"). Aber wenn ich die App beende, gibt es eine rote Linie auf Klasse AppDelegate. Ich verstehe nicht, was ist das Problem hier.Ich habe dies mit der Lösung der Frage "Getting location for an iOS app when it is in the background and even killed" hier getan, aber es funktioniert nicht in IOS 9.Bitte helfen Sie mir oder sagen Sie mir die andere Lösung.Standort wird aktualisiert, auch wenn die App beendet wurde/beendet wurde

AKTUALISIERT CODE -

class AppDelegate: UIResponder, UIApplicationDelegate, CLLocationManagerDelegate { 

var window: UIWindow? 
var locationManager: CLLocationManager? 
var significatLocationManager : CLLocationManager? 

func application(application: UIApplication, didFinishLaunchingWithOptions launchOptions: [NSObject : AnyObject]?) -> Bool { 
    if(UIApplication.sharedApplication().backgroundRefreshStatus == UIBackgroundRefreshStatus.Available){ 
     print("yessssss") 
    }else{ 
     print("noooooo") 
    } 

    if let launchOpt = launchOptions{ 
     if (launchOpt[UIApplicationLaunchOptionsLocationKey] != nil) { 
      self.significatLocationManager = CLLocationManager() 
      self.significatLocationManager?.delegate = self 
      self.significatLocationManager?.requestAlwaysAuthorization() 
      if #available(iOS 9.0, *) { 
       self.significatLocationManager!.allowsBackgroundLocationUpdates = true 
      } 
      self.significatLocationManager?.startMonitoringSignificantLocationChanges() 

     }else{ 

      self.locationManager   = CLLocationManager() 
      self.locationManager?.delegate = self 
      self.locationManager?.requestAlwaysAuthorization() 
      if #available(iOS 9.0, *) { 
       self.locationManager!.allowsBackgroundLocationUpdates = true 
      } 

      self.locationManager?.startMonitoringSignificantLocationChanges() 
     } 

    }else{ 

     self.locationManager   = CLLocationManager() 
     self.locationManager?.delegate = self 
     self.locationManager?.requestAlwaysAuthorization() 
     if #available(iOS 9.0, *) { 
      self.locationManager!.allowsBackgroundLocationUpdates = true 
     } 

     self.locationManager?.startMonitoringSignificantLocationChanges() 

    } 

    return true 
} 



func locationManager(manager: CLLocationManager, didUpdateLocations locations: [CLLocation]){ 

    let locationArray = locations as NSArray 
    let locationObj = locationArray.lastObject as! CLLocation 
    let coord = locationObj.coordinate 
     } 


func applicationDidEnterBackground(application: UIApplication) { 

    if self.significatLocationManager != nil { 

     self.significatLocationManager?.startMonitoringSignificantLocationChanges() 
    }else{ 

     self.locationManager?.startMonitoringSignificantLocationChanges() 
    } 


} 
+1

Mögliche Duplikate: http://stackoverflow.com/questions/30396367/getting-location-for-an-ios-app-when-it-is-in-the-background -und-even-killed –

+1

* "es gibt eine rote Linie auf Klasse AppDelegate" * - das ist einer der schlimmsten und am wenigsten hilfreiche Fehlerbeschreibungen. Bitte poste die Fehlermeldung hier. Wenn Sie Ihre App über Xcode laufen lassen, ist es normal, dass die "rote Linie" angezeigt wird, wenn Sie die App zwangsweise beenden. – luk2302

+0

Ja, ich beende die App zwangsweise und dieser rote Zeilenfehler wird angezeigt, aber ich bekomme keine Standortaktualisierung beim Beenden der App. Was ist das Problem hier ?? – Kirti

Antwort

0

prüfen dieses Tutorial: http://mobileoop.com/getting-location-updates-for-ios-7-and-8-when-the-app-is-killedterminatedsuspended

und der Quellcode hier: https://github.com/voyage11/GettingLocationWhenSuspended

Hoffe, dass Sie Ihre Antwort bekommen. Es funktioniert für mich. Jetzt versuche ich eine HTTP-Anfrage zu machen, wenn die 'didUpdateLocations'-Funktion aufgerufen wird, damit ich den aktuellen Standort des Benutzers auf dem Server speichern kann, wenn die App nicht läuft (Suspended/terminated/killed).

Ich denke nicht, dass Apple http-Anfrage machen darf, wenn die App ausgesetzt/beendet wird. Aber wenn der Server die Anforderung der Standortaktualisierung erhalten hat, dann bin ich gut damit. Weil ich keine Antwort vom Server brauche. Brauchen Sie viel Test ....

Verwandte Themen