2012-05-24 2 views
0

Я пытаюсь запланировать кучу уведомлений, когда приложение переходит на задний план. Следующий код работает хорошо в iPhone Simulator ...Что не так с этим методом applicationDidEnterBackground?

- (void)applicationDidEnterBackground:(UIApplication *)application { 

NSLog(@"applicationDidEnterBackground"); 

bgTask = [application beginBackgroundTaskWithExpirationHandler:^{ 
    // Clean up any unfinished task business by marking where you. 
    // stopped or ending the task outright. 
    [application 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, preferably in chunks. 

    // Get notifications count 
    int notificationsCount   = [[[UIApplication sharedApplication] scheduledLocalNotifications] count]; 

    // Calculate the number of possible remaining notifications (maximum 64 per application) 
    int notificationsRemaining  = 64 - notificationsCount; 

    if (notificationsCount > 0) {  
     // Get the last notification currently scheduled 
     UILocalNotification *lastNotif = [[[UIApplication sharedApplication] scheduledLocalNotifications] lastObject]; 

     // If the last notification is not repeating notification 
     if (lastNotif.repeatInterval == 0) { 

      // Get the last notification's values 
      NSDictionary *userInfo   = lastNotif.userInfo; 
      NSInteger interval    = [[userInfo objectForKey:@"interval"] intValue]; 
      NSDate *fireDate    = lastNotif.fireDate; 
      NSTimeZone *timeZone   = lastNotif.timeZone; 
      NSString *alertBody    = lastNotif.alertBody; 
      bool hasAction     = lastNotif.hasAction; 
      NSString *alertAction   = lastNotif.alertAction; 
      NSString *soundName    = lastNotif.soundName; 

      // Schedule the remaining notifications 
      for (int i = 1 ; i < notificationsRemaining+1 ; i++) { 

       // Allocate new local notification to be scheduled 
       UILocalNotification *notif  = [[UILocalNotification alloc] init]; 

       // If notif is nil go back one step and try again 
       if (notif == nil) { 
        i--; 
       } else {      
        // Set up the newly created notification 
        notif.fireDate      = [fireDate dateByAddingTimeInterval: 10 * interval * i]; 
        notif.timeZone      = timeZone; 
        notif.alertBody      = alertBody; 
        notif.hasAction      = hasAction; 
        notif.alertAction     = alertAction; 
        notif.soundName      = soundName; 
        notif.userInfo      = userInfo; 
        notif.applicationIconBadgeNumber += 1; 

        // Schedule the newly created notification 
        [[UIApplication sharedApplication] scheduleLocalNotification:notif]; 

        [notif release]; 

       } 

      }   
     }  
    }  

    NSLog(@"notifications: %@", [[UIApplication sharedApplication] scheduledLocalNotifications]); 

    [application endBackgroundTask:bgTask]; 
    bgTask = UIBackgroundTaskInvalid; 
}); 

}

..., но не в IPod Touch 2 с ошибками:

Thu May 24 14:03:18 unknown TestApp[599] <Warning>: applicationDidEnterBackground 
Thu May 24 14:03:18 unknown TestApp[599] <Warning>: applicationWillTerminate 
Thu May 24 14:03:18 unknown com.apple.launchd[1] <Notice>: (UIKitApplication:my.test.TestApplicationWithPhoneGap[0x1c13]) Bug: launchd_core_logic.c:3252 (24226):3 
Thu May 24 14:03:18 unknown com.apple.launchd[1] <Notice>: (UIKitApplication:my.test.TestApplicationWithPhoneGap[0x1c13]) Bug: launchd_core_logic.c:2681 (24226):10 
Thu May 24 14:03:18 unknown com.apple.launchd[1] <Notice>: (UIKitApplication:my.test.TestApplicationWithPhoneGap[0x1c13]) Working around 5020256. Assuming the job crashed. 
Thu May 24 14:03:18 unknown com.apple.launchd[1] <Warning>: (UIKitApplication:my.testTestApplicationWithPhoneGap[0x1c13]) Job appears to have crashed: Segmentation fault 
Thu May 24 14:03:18 unknown com.apple.debugserver-48[597] <Warning>: 1 [0255/1403]: error: ::read (4, 0x2fee59f0, 1024) => -1 err = Bad file descriptor (0x00000009) 
Thu May 24 14:03:18 unknown SpringBoard[24] <Warning>: Application 'TestApp' exited abnormally with signal 11: Segmentation fault 
Thu May 24 14:03:24 unknown SpringBoard[24] <Warning>: Unable to cancel system wake for 2012-05-24 11:03:09 +0000. IOPMCancelScheduledPowerEvent() returned 0xe00002f0 

Любые идеи, почему это не работает на реальном устройстве, любые идеи, как это исправить?

EDITED ОТВЕТ:
Кажется, что старшее поколение IPod Touch и iPhone не поддерживает этот

ответ

2

Смотрите, если устройство поддерживает фоновый режим?

+0

звучит возможная причина ... Как мне это сделать? У меня iPod Touch 2, iOS 4.2.1. – micadelli

+0

Кнопка двойного нажатия на дом, если устройство поддерживает фоновый режим - появится меню с запущенными приложениями. – Moonkid

+0

Вот чего я боялся ... никакой поддержки. :) благодаря – micadelli

Смежные вопросы