当我的应用程序处于终止状态时,似乎没有启动并调用位置更新。
因为我有点难以测试不起作用的东西(当你不得不在办公室内来回移动试图触发重要的位置变化时,使用真实的设备并不容易),是否有在应用程序关闭时模拟模拟器中位置变化的方法?
我已经尝试过使用模拟器>调试>位置> [City Bicyce Ride,...]但它似乎只在应用程序运行时才有效。我甚至尝试创建一个方案,在编译后应用程序不会自动启动。
您对如何调试此类问题有任何建议吗? (到现在为止,我只是在每个应用程序启动时都会记录单独的文件,即使不幸的是,当处于关闭状态时应用程序不会在后台启动)
这是我的app delegate中的代码:
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()
}
上述代码的行为是应用程序仅在活动时监视用户位置更改。 查看下面的图像很清楚,模拟器似乎继续移动Bicycle Ride的位置,但AppDelegate CLLocationManagerDelegate的locationManager(管理器:CLLocationManager,didUpdateLocations位置:[CLLocation])在应用程序终止时不会被调用或在后台:
答案 0 :(得分:0)
您是否尝试过自定义位置而不是City Bycle Ride?我的一个应用程序我使用区域监控,如果我手动给出位置,那么即使我锁定模拟器它也能正常工作。