首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >在iOS背景中获取位置

在iOS背景中获取位置
EN

Stack Overflow用户
提问于 2011-07-08 06:40:33
回答 1查看 2.3K关注 0票数 2

我想每隔X分钟在后台获得我的位置,我使用的是一个在NSLog上运行良好的线程,它打印一个字符串。然而,当我调用我的locationManager时,它似乎不起作用。

这是我的代码:

代码语言:javascript
复制
- (void)threadEntryPoint:(ActualizarPosicionGPS2AppDelegate *)paramSender{
    NSAutoreleasePool *pool = [[NSAutoreleasePool alloc] init];

    while([[NSThread currentThread] isCancelled] == NO){
        [NSThread sleepForTimeInterval:10.0f];
        if ([[NSThread currentThread] isCancelled] == NO &&
            [[UIApplication sharedApplication] backgroundTimeRemaining] != DBL_MAX) {
            [self getLocation];
        }
    }

    [pool release];
}

在应用程序中:didFinishLaunchingWithOptions:

代码语言:javascript
复制
[NSThread detachNewThreadSelector:@selector(threadEntryPoint:) toTarget:self withObject:self];

然后,我得到了以下内容:

代码语言:javascript
复制
-(void) endTaskWidthIdentifier:(NSNumber *)paramIdentifier{
    UIBackgroundTaskIdentifier identifier = [paramIdentifier integerValue];
    [[UIApplication sharedApplication] endBackgroundTask:identifier];
}
- (void)applicationDidEnterBackground:(UIApplication *)application {
    self.backgroundTaskIdentifier = [application beginBackgroundTaskWithExpirationHandler:^{
        NSNumber *backgroundTask = [NSNumber numberWithInteger:self.backgroundTaskIdentifier];
        [self performSelectorOnMainThread:@selector(endTaskWidthIdentifier:) withObject:backgroundTask waitUntilDone:YES];
        self.backgroundTaskIdentifier = UIBackgroundTaskInvalid;
    }];

}

重要的是,getLocation是这样的:

代码语言:javascript
复制
-(void)getLocation{
    if(nil==locationManager)
        locationManager = [[CLLocationManager alloc] init];

    [locationManager setDelegate:self];
    [locationManager setDistanceFilter:kCLLocationAccuracyHundredMeters];
    [locationManager setDesiredAccuracy:kCLLocationAccuracyBest];

    [locationManager startUpdatingLocation];
    NSLog(@"ouch");
    if(locationManager.location!=nil){
        NSLog(@"obtenido %f, %f",locationManager.location.coordinate.latitude,locationManager.location.coordinate.longitude);
    }
}

locationManager.location总是返回nil,所以它不会得到当前的位置。

我做错什么了吗?也许我有一个错误的概念!

非常感谢!

EN

回答 1

Stack Overflow用户

发布于 2011-08-01 18:47:54

在这篇文章中可以看到我的答案。

How do I get a background location update every n minutes in my iOS application?

您可能还想检查为什么没有调用您的委托方法

如果您对它感到满意,请将您的项目发送到xs2bush@yahoo.com。我会执行代码,看看哪里出了问题。

票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/6617988

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档