2012-05-24 95 views
0

我想安排一堆通知,當應用程序去背景。下面的代碼工作以及在iPhone模擬器......這個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