我在后台阅读了一些关于app运行的问题,我知道有可能这样做,比如运行监控GPS数据的应用程序。我想确认是否可以在后台制作一个不停的程序,每当GPS改变时,启动一些其他模块运行一段时间。之后,这个程序仍然在后台运行?
我听到一些事情:Adobe Air在移动设备上运行应用程序超过10分钟。所以任何人都可以确认是否有可能在普通的iOS上做上述要求?谢谢!
答案 0 :(得分:0)
Use this code to run in background in app delegate class
- (void)applicationDidEnterBackground:(UIApplication *)application
{
UIApplication *app = [UIApplication sharedApplication];
UIBackgroundTaskIdentifier bgTask = 0;
bgTask = [app beginBackgroundTaskWithExpirationHandler:^{
[app endBackgroundTask:bgTask];
}];
// 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.
}
To get gps data call gps location manager inside the above method
eg:
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{
locationmanager = [[CLLocationManager alloc]init];
[locationmanager setDelegate:self];
locationmanager.distanceFilter = kCLDistanceFilterNone;
[locationmanager setDesiredAccuracy:kCLLocationAccuracyBest];
return YES;
}
- (void)applicationDidEnterBackground:(UIApplication *)application
{
[locationmanager startUpdatingLocation];
UIApplication *app = [UIApplication sharedApplication];
UIBackgroundTaskIdentifier bgTask = 0;
bgTask = [app beginBackgroundTaskWithExpirationHandler:^{
[app endBackgroundTask:bgTask];
}];
}
- (void)locationManager:(CLLocationManager *)manager
didUpdateToLocation:(CLLocation *)newLocation
fromLocation:(CLLocation *)oldLocation{
latitude = [[NSString alloc]initWithFormat:@"%g",newLocation.coordinate.latitude];
longitude = [[NSString alloc]initWithFormat:@"%g",newLocation.coordinate.longitude];
}