Search code examples
iphoneiosactionscriptbackgroundair

Can you run program in iphone background like this


I read some questions about app run in background and I know that there could b some possibilities to do so, like run an app monitoring the GPS data. I want to confirm is it possible to make a program like that non-stop at background and whenever GPS changes it start some other modules run for a while. After that this program still running in background ?

I hear some thing about : Adobe Air on mobile and run apps more than 10 mins. So can anyone confirm is it possible to do above requirement in normal iOS ? Thank you !


Solution

  •     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];
        }