我正在尝试每天在给定时间触发一个方法。我尝试了一些方法,但我无法真正使其发挥作用。任何意见,将不胜感激。此外,如果无论应用程序是否打开它都会触发,那就更理想了。这可能吗?
UI本地通知 http://developer.apple.com/library/ios/#documentation/NetworkingInternet/Conceptual/RemoteNotificationsPG/IPhoneOSClientImp/IPhoneOSClientImp.html#//apple_ref/doc/uid/TP40008194-CH103-SW13当您的应用程序在后台运行时,将允许您触发通知(但不是方法),或者如果应用程序在前台运行,则将调用您实现的委托方法(application:didReceiveLocalNotification :),或者将调用您的方法当用户响应警报时,必须实现 (application:didFinishLaunchingWithOptions:)。除此之外,当应用程序不在前台时,您将无法调用方法,您只能触发通知(可以显示徽章、播放声音等)。
顺便说一句,如果您想要此功能,请考虑向苹果提交错误报告。我希望能够根据本地通知在后台运行方法,而无需等待用户先响应。
看苹果的示例代码:
- (void)scheduleNotificationWithItem:(ToDoItem *)item interval:(int)minutesBefore {
NSCalendar *calendar = [NSCalendar autoupdatingCurrentCalendar];
NSDateComponents *dateComps = [[NSDateComponents alloc] init];
[dateComps setDay:item.day];
[dateComps setMonth:item.month];
[dateComps setYear:item.year];
[dateComps setHour:item.hour];
[dateComps setMinute:item.minute];
NSDate *itemDate = [calendar dateFromComponents:dateComps];
[dateComps release];
UILocalNotification *localNotif = [[UILocalNotification alloc] init];
if (localNotif == nil)
return;
localNotif.fireDate = [itemDate addTimeInterval:-(minutesBefore*60)];
localNotif.timeZone = [NSTimeZone defaultTimeZone];
localNotif.alertBody = [NSString stringWithFormat:NSLocalizedString(@"%@ in %i minutes.", nil),
item.eventName, minutesBefore];
localNotif.alertAction = NSLocalizedString(@"View Details", nil);
localNotif.soundName = UILocalNotificationDefaultSoundName;
localNotif.applicationIconBadgeNumber = 1;
NSDictionary *infoDict = [NSDictionary dictionaryWithObject:item.eventName forKey:ToDoItemKey];
localNotif.userInfo = infoDict;
[[UIApplication sharedApplication] scheduleLocalNotification:localNotif];
[localNotif release];
}
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)