Es scheint, dass meine App nicht gestartet wird und mit Standortaktualisierungen aufgerufen wird, wenn sie sich in einem abgeschlossenen Status befindet.iOS-Debug-Standortüberwachung beim Schließen der App vom Simulator
Da es ein bisschen schwierig für mich ist zu testen, was nicht funktioniert (mit einem echten Gerät ist nicht wirklich einfach, wenn Sie hin und her in einem Büro bewegen müssen, um eine signifikante Standortänderung auszulösen), ist da eine Möglichkeit, Standortänderungen im Simulator zu simulieren, während die App geschlossen ist?
Ich habe bereits versucht, mit dem Simulator> Debug> Ort> [City Bicyce Ride, ...] aber es scheint, dass es nur funktioniert, wenn die App ausgeführt wird. Ich habe sogar versucht, ein Schema zu erstellen, bei dem die App nach dem Kompilieren nicht automatisch gestartet wird.
Haben Sie Vorschläge zum Debuggen dieser Art von Problemen? (Inzwischen bin ich bei jedem Programmstart auf separate Dateien nur anmelden, obwohl leider die App nicht im Hintergrund gestartet wird, wenn in einem geschlossenen Zustand ist)
Dies ist der Code in meinem AppDelegate:
lazy var locationManagerFitness: CLLocationManager! = {
let manager = CLLocationManager()
manager.desiredAccuracy = kCLLocationAccuracyBest
manager.distanceFilter = 1.0
manager.activityType = CLActivityType.Fitness
manager.delegate = self
manager.requestAlwaysAuthorization()
return manager
}()
func startLocationMonitoring()
{
locationManagerFitness.stopMonitoringSignificantLocationChanges()
locationManagerFitness.startUpdatingLocation()
}
func startLocationMonitoringSignificantChanges()
{
locationManagerFitness.stopUpdatingLocation()
locationManagerFitness.startMonitoringSignificantLocationChanges()
}
// MARK: - CLLocationManagerDelegate
func locationManager(manager: CLLocationManager, didUpdateLocations locations: [CLLocation])
{
if manager == locationManagerFitness
{
log.debug("locationManagerFitness:")
}
for newLocation in locations
{
saveLocation(newLocation)
if UIApplication.sharedApplication().applicationState == .Active {
log.debug("App is active. New location is \(newLocation)")
} else {
log.debug("App is in background. New location is \(newLocation)")
}
}
}
func saveLocation(location: CLLocation) -> Location {
let entity = NSEntityDescription.entityForName("Location",
inManagedObjectContext:managedObjectContext)
let locationCD = NSManagedObject(entity: entity!,
insertIntoManagedObjectContext: managedObjectContext) as! Location
locationCD.setValue(location.coordinate.latitude, forKey: "latitude")
locationCD.setValue(location.coordinate.longitude, forKey: "longitude")
locationCD.setValue(NSDate(), forKey: "creationDate")
do {
try managedObjectContext.save()
} catch let error as NSError {
print("Could not save \(error), \(error.userInfo)")
}
return locationCD
}
func application(application: UIApplication, didFinishLaunchingWithOptions launchOptions: [NSObject: AnyObject]?)
-> Bool {
//Logs
let documentDirectoryURL = try! NSFileManager.defaultManager().URLForDirectory(.DocumentDirectory, inDomain: .UserDomainMask, appropriateForURL: nil, create: true)
let dayTimePeriodFormatter = NSDateFormatter()
dayTimePeriodFormatter.dateFormat = "hh:mm_dd-MM-yyyy"
let dateString = dayTimePeriodFormatter.stringFromDate(NSDate())
let logURL = documentDirectoryURL.URLByAppendingPathComponent("log_\(dateString).txt")
log.setup(.Debug, showThreadName: true, showLogLevel: true, showFileNames: true, showLineNumbers: true, writeToFile: logURL, fileLogLevel: .Debug)
log.debug("Starting app...")
// StatusBar
UIApplication.sharedApplication().statusBarStyle = .LightContent
switch CLLocationManager.authorizationStatus()
{
case .AuthorizedAlways:
if let _ = launchOptions?[UIApplicationLaunchOptionsLocationKey]
{
startLocationMonitoringSignificantChanges()
}
default:
break;
}
log.debug("App started!")
return true
}
func applicationDidEnterBackground(application: UIApplication) {
// Use this method to release shared resources, save user data, invalidate timers, and store enough application state information to restore your application to its current state in case it is terminated later.
// If your application supports background execution, this method is called instead of applicationWillTerminate: when the user quits.
log.debug("startLocationMonitoringSignificantChanges")
startLocationMonitoringSignificantChanges()
}
func applicationDidBecomeActive(application: UIApplication) {
// Restart any tasks that were paused (or not yet started) while the application was inactive. If the application was previously in the background, optionally refresh the user interface.
log.debug("startLocationMonitoring")
startLocationMonitoring()
}
Das Verhalten des obigen Codes besteht darin, dass die App Benutzerstandortänderungen nur überwacht, wenn sie aktiv ist. Wenn Sie das folgende Bild sehen, scheint der Simulator den Standort des Radtour zu verschieben, jedoch wird der locationManager des AppDelegate CLLocationManagerDelegate (Manager: CLLocationManager, didUpdateLocations: [CLLocation]) nicht aufgerufen, während die App beendet oder im Hintergrund ist :