我正在编写一个需要高精度和低频率的背景位置更新的应用程序.该解决方案似乎是一个后台NSTimer任务,它启动位置管理器的更新,然后立即关闭.之前已经问过这个问题:
但我还没有得到最起码的例子.在尝试了上述接受的答案的每一个排列后,我总结了一个起点.输入背景:
- (void)applicationDidEnterBackground:(UIApplication *)application
{
self.bgTask = [[UIApplication sharedApplication] beginBackgroundTaskWithExpirationHandler:^{
NSLog(@"ending background task");
[[UIApplication sharedApplication] endBackgroundTask:self.bgTask];
self.bgTask = UIBackgroundTaskInvalid;
}];
self.timer = [NSTimer scheduledTimerWithTimeInterval:60
target:self.locationManager
selector:@selector(startUpdatingLocation)
userInfo:nil
repeats:YES];
}
Run Code Online (Sandbox Code Playgroud)
和委托方法:
- (void)locationManager:(CLLocationManager *)manager
didUpdateToLocation:(CLLocation *)newLocation
fromLocation:(CLLocation *)oldLocation {
NSLog(@"%@", newLocation);
NSLog(@"background time: %f", [UIApplication sharedApplication].backgroundTimeRemaining);
[self.locationManager stopUpdatingLocation];
}
Run Code Online (Sandbox Code Playgroud)
当前行为是backgroundTimeRemaining从180秒减少到零(记录位置时),然后执行到期处理程序,不再生成进一步的位置更新.如何修改上述代码以便在后台无限期地接收定期位置更新?
更新:我的目标是iOS 7,似乎有一些证据表明后台任务的行为有所不同:
objective-c core-location ios background-task ios-background-mode
我有位置服务的后台模式,目标是每30分钟向服务器发送一个位置(经度和纬度).现在我在控制台中打印相同的内容.它似乎工作了一段时间,但我想知道如何在这种情况下使用NSTimer.我应该从哪里打电话呢?
import UIKit
import CoreLocation
@UIApplicationMain
class AppDelegate: UIResponder, UIApplicationDelegate, CLLocationManagerDelegate {
var window: UIWindow?
var locationManager = CLLocationManager()
func application(application: UIApplication, didFinishLaunchingWithOptions launchOptions: [NSObject: AnyObject]?) -> Bool {
// Override point for customization after application launch.
return true
}
func applicationWillResignActive(application: UIApplication) {
// Sent when the application is about to move from active to inactive state. This can occur for certain types of temporary interruptions (such as an incoming phone call or SMS message) or when the user …Run Code Online (Sandbox Code Playgroud) 我只想按需跟踪用户当前位置。我只需要初始位置,不需要持续更新。我认为最好的方法是向用户发送静默通知,以便一次获取用户的当前位置。接收静默通知效果很好,但 LocationManager 无法对位置进行初始修复。我的位置管理器委托类正常实例化,但在调用位置管理器方法 startUpdatingLocation() 后的任何时候都不会触发 didUpdateLocations 函数。当应用程序从 AppDelegate:didFinishLaunchingWithOptions 正常启动时,LocationManager 会毫无问题地更新用户位置。
我正在 Xcode 6.3.2 到 iOS 8.3 上进行开发
我的 AppDelegate:DidReceiveRemoteNotification 看起来像这样:
func application(application: UIApplication, didReceiveRemoteNotification userInfo: [NSObject : AnyObject], fetchCompletionHandler handler: (UIBackgroundFetchResult) -> Void) {
var bgTask = bgManager()
bgTask.registerBackgroundTask()
LocManager.locationMngr.startUpdatingLocation() // LocManager is global variable
let delayInSeconds = 8.0
let popTime = dispatch_time(DISPATCH_TIME_NOW, Int64(delayInSeconds * Double(NSEC_PER_SEC)))
dispatch_after(popTime, dispatch_get_main_queue()) {
println(LocManager.updated)
bgTask.endBackgroundTask()
handler(UIBackgroundFetchResult.NewData)
}
}
Run Code Online (Sandbox Code Playgroud)
我首先尝试仅使用以下行:
LocManager.locationMngr.startUpdatingLocation()
Run Code Online (Sandbox Code Playgroud)
但当它不起作用时,我添加了后台任务和dispatch_after,以确保我的位置管理器有足够的时间在终止之前检索第一个位置更新。
这是 LocationManager 类:
class LocationManager: NSObject, CLLocationManagerDelegate {
let locationMngr:CLLocationManager
var coord:CLLocationCoordinate2D
var …Run Code Online (Sandbox Code Playgroud) core-location apple-push-notifications appdelegate swift ios8.3