iOS越狱-如何在后台获取位置



我写了一个小应用程序,它作为守护进程启动。它基本上只会输出手机的GPS位置。

主.m:

int main(int argc, char *argv[]) {
NSAutoreleasePool *p = [[NSAutoreleasePool alloc] init];
LocationController *obj = [[LocationController alloc] init];
[obj getLocation];    
NSRunLoop *runLoop = [NSRunLoop currentRunLoop];
[runLoop run];
[p drain];
return 0;
}

LocationController.m

@implementation LocationController
@synthesize locationManager;
-(id)init {
    self = [super init];
    if(self) {
        trackingGPS = false;
        locationManager = [[CLLocationManager alloc] init];
        locationManager.delegate = self;
    }
    return self;
}
-(void)getLocation {
    [locationManager startUpdatingLocation];
}
- (void)locationManager:(CLLocationManager *)manager didUpdateToLocation:(CLLocation     *)newLocation fromLocation:(CLLocation *)oldLocation {
    NSLog(@"NEW LOCATION :: %@", newLocation);
}
- (void)locationManager:(CLLocationManager *)manager didFailWithError:(NSError *)error {
    NSLog(@"LOCATION ERROR : %@", error);
}
-(void) dealloc {
    [locationManager release];
    [super dealloc];
}
@end

因此,如果我在跳板上手动运行应用程序,它会正常工作,并记录GPS位置。。。至少15-20秒。。。然后跳板终止应用程序,因为它没有重新启动-这是一种预期的行为。

但是,如果我在启动时启动应用程序(launchDaemon),它也会很好地启动,但永远不会调用委托函数"didUpdateToLocation"!!

我在iOS 5上,所以不确定问题出在哪里。任何帮助都将不胜感激。

THX!!

与其调用"startUpdatingLocation",不如使用startMonitoringSignificantLocationChanges。每当用户的位置发生重大变化时,它都会调用您的应用程序,您将有一些时间进行后台处理,并确定是否要打开应用程序。

如果用户进入了你想要监控的某个区域,你也可以让设备调用你的应用程序。

有关详细信息,请参阅此问题。

最新更新