gpt4 book ai didi

iphone - 这个 applicationDidEnterBackground 方法有什么问题?

转载 作者:行者123 更新时间:2023-11-28 22:56:17 24 4
gpt4 key购买 nike

我正在尝试在应用程序进入后台时安排一堆通知。以下代码在 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 不支持这个

最佳答案

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

关于iphone - 这个 applicationDidEnterBackground 方法有什么问题?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/10736427/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com