作者热门文章
- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我需要知道是否可以创建一个新线程来处理设置本地通知。
我的应用严重依赖于这些通知,因此我希望在手机设置通知时使应用正常运行。
示例:
(现在)
您启动应用程序,应用程序会卡在启动屏幕上以设置本地通知,然后启动。
(我想要)
设置本地通知后,应用程序将启动并可用。
我也需要一些示例代码,请:)
(郑重声明,出于我自己的原因,我每次应用程序进入前台时都会设置 60 个本地通知...)
谢谢!!
最佳答案
是的,这是可以做到的,我一直这样做:
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
// Add the navigation controller's view to the window and display.
[NSThread detachNewThreadSelector:@selector(scheduleLocalNotifications) toTarget:self withObject:nil];
[window addSubview:navigationController.view];
[window makeKeyAndVisible];
return YES;
}
-(void) scheduleLocalNotifications
{
NSAutoreleasePool *pool = [[NSAutoreleasePool alloc] init];
for (int i = 0; i < 60; i++)
{
UILocalNotification *localNotif = [[UILocalNotification alloc] init];
if (localNotif == nil)
return;
NSDate *sleepDate = [[NSDate date] dateByAddingTimeInterval:i * 60];
NSLog(@"Sleepdate is: %@", sleepDate);
localNotif.fireDate = sleepDate;
NSLog(@"fireDate is %@",localNotif.fireDate);
localNotif.timeZone = [NSTimeZone defaultTimeZone];
localNotif.alertBody = [NSString stringWithFormat:NSLocalizedString(@"This is local notification %i"), i];
localNotif.alertAction = NSLocalizedString(@"View Details", nil);
localNotif.soundName = UILocalNotificationDefaultSoundName;
localNotif.applicationIconBadgeNumber = 1;
[[UIApplication sharedApplication] scheduleLocalNotification:localNotif];
NSLog(@"scheduledLocalNotifications are %@", [[UIApplication sharedApplication] scheduledLocalNotifications]);
[localNotif release];
}
[pool release];
}
取 self 现在正在进行的一个项目,我可以确认它按预期工作。
编辑:scheduleLocalNotifications
中的示例发生泄漏,因为缺少对 NSAutoreleasePool
的处理 - 现在已将其添加到示例中。
关于iphone - 在新线程上设置本地通知?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/4316880/
嘿。本周的一个教程,其中一个问题要求通过使用其他函数 formatLine 和 formatList 创建一个函数 formatLines,以格式化行列表。 我的代码是这样的; type Line =
我是一名优秀的程序员,十分优秀!