приложение запускается в фоновом режиме - PullRequest
0 голосов
/ 08 августа 2011

Я хочу разработать приложение, которое получает текущее местоположение в фоновом режиме приложения и генерирует в соответствии с конкретным событием местоположения,

Итак, как я могу сделать приложение, которое перейдет в фоновый режим и получит продолжение местоположения.

Ответы [ 2 ]

2 голосов
/ 08 августа 2011

Apple написала демонстрационное приложение, делающее в точности то, что спрашивают: Хлебная крошка

0 голосов
/ 08 августа 2011
- (CLLocationManager *)locationManager
{                
    if (locationManager != nil)
        return locationManager;

    locationManager = [[CLLocationManager alloc] init];

 //locationManager.desiredAccuracy = kCLLocationAccuracyNearestTenMeters;

 locationManager.delegate = self;

 return locationManager;        
}  




- (void)applicationDidEnterBackground:(UIApplication *)application
{
    UIApplication*    app = [UIApplication sharedApplication];
    // Request permission to run in the background. Provide an
    // expiration handler in case the task runs long.
    NSAssert(bgTask == UIBackgroundTaskInvalid, nil);
    bgTask = [app beginBackgroundTaskWithExpirationHandler:^{
        // Synchronize the cleanup call on the main thread in case
        // the task actually finishes at around the same time.
        dispatch_async(dispatch_get_main_queue(), ^{
        if (bgTask != UIBackgroundTaskInvalid)
        {
            [app endBackgroundTask:bgTask];
            bgTask = UIBackgroundTaskInvalid;
        });
    }];
// Start the long-running task and return immediately.
dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^{
    // Do the work associated with the task.
    // Synchronize the cleanup call on the main thread in case
    // the expiration handler is fired at the same time.
    dispatch_async(dispatch_get_main_queue(), ^{
        if (bgTask != UIBackgroundTaskInvalid)
        {
            [app endBackgroundTask:bgTask];
            bgTask = UIBackgroundTaskInvalid;
        }
    });
});

}

  - (void) locationManager:(CLLocationManager *)manager didUpdateToLocation:(CLLocation *)newLocation fromLocation:(CLLocation *)oldLocation`  

{
NSLog(@"location changed");<br> UIApplication* app = [UIApplication sharedApplication];

NSArray*    oldNotifications = [app scheduledLocalNotifications];

// Clear out the old notification before scheduling a new one.

if ([oldNotifications count] > 0)

    [app cancelAllLocalNotifications];

  // Create a new notification.

UILocalNotification* alarm = [[[UILocalNotification alloc] init] autorelease];

if (alarm)

{
     alarm.timeZone = [NSTimeZone defaultTimeZone];

    alarm.repeatInterval = 0;

    alarm.soundName = @"b.wav";

    alarm.alertBody = @"Location changed!";


    [app scheduleLocalNotification:alarm];

}

}

Добро пожаловать на сайт PullRequest, где вы можете задавать вопросы и получать ответы от других членов сообщества.
...