这个applicationDidEnterBackground方法有什么问题?

时间:2012-05-24 11:16:48

标签: iphone ios notifications background-process

我正在尝试在应用转到后台时安排一堆通知。以下代码在iPhone模拟器中运行良好...

- (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

任何想法为什么这不适用于真实设备,任何想法如何解决这个问题?

编辑回答:
似乎老一代iPod Touch和iPhone不支持这个

1 个答案:

答案 0 :(得分:2)

查看您的设备是否支持后台模式?