2012-02-09 24 views
3

我寫了一個作爲守護進程開始的小應用程序。它基本上只會輸出手機的GPS位置。iOS越獄 - 如何在後臺獲取位置

的main.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 !!

回答

1

而不是調用「startUpdatingLocation」,您應該使用startMonitoringSignificantLocationChanges。每當用戶的位置發生重大變化時,它都會調用您的應用程序,並且您將有一些時間做一些後臺處理並確定是否要打開您的應用程序。

如果用戶進入您希望監控的某個區域,您也可以讓設備調用您的應用程序。

有關更多詳細信息,請參見question

+0

嘿aguy。 Thx的提示,但它仍然無法工作!委託函數未被調用。但我認爲這在iOS 5+上已經不可行了 – Pascal 2012-03-01 23:38:48