开发者

Subclassing NSCalendar?

I'm trying to make a subclass of NSCalendar, but without much luck. I've tried two ways:

  1. thorough categories. I can create a modified object and a test method that I added (not part of NSCalendar) works just fine, but overriden methods don't work - e.g. if I call dateFromComponents:, the NSCalendar method is apparently called, not what I wrote in the category definition

  2. regular subclass. In this case I can't even create an instance: initWithCalendarIdentifier throws a _CFRequireConcreteImplementation exception. Initially I only had a couple of methods overriden in the subclass, but then I've overriden all of the (documented) NSCalendar methods - and the result is the same.

Is it even possible to subclass NSCalendar?

The reason I want to do it is to make repeating Local Notifications with non-standard repeat intervals. The default functionality allows Local Notifications to repeat every [calendar unit], but I need irregular intervals - e.g. I can emulate "every 15 minutes" notifications by creating 4 "every hour" notifications 15 minutes apart, but I need them to fire at, say T+20, T+22, T+24, T+44, T+46, T+66 minutes and so on (T is the start time) - the interval between notifications is 20 minutes, then 2 minutes, then 20 minutes again and so on.

I was hoping that, since UILocalNotification wants an NSCalendar (in repeatCalendar property) to calculate when to fire the next notification, I can achieve what 开发者_JAVA技巧I want by overriding, say, dateFromComponents to just return [NSDate dateWithIntervalSinceNow] with interval alternating between 20 and 2 minutes - but my cunning plan seems to have a major problem because of the inability to subclass NSCalendar.

Edit: This whole thing is needed for when the app is in the background. In the foreground I use timers, just like No one in particular suggests.


Even if you do manage to subclass NSCalendar, I doubt it'll work. I strongly suspect that local notifications are scheduled by SpringBoard, and I don't think it's going to launch/resume your app every time it needs to see what the next fire date is.

The first step is to find out what UILocalNotification is doing with your calendar. The easiest way to do this is to write an NSObject subclass which forwards undefined method calls and prints log messages (respondsToSelector:, methodSignatureForSelector:, forwardInvocation:). Generally, you want something like if ([super respondsToSelector:selector]) { return [super ...]; } else { return [target ...]; }.

Then, the easiest hack is to write a class which implements the necessary methods of NSCalendar for your purposes.

You might also consider calling [super init]; -[NSCalendar initWithCalendarIdentifier:] is likely to return a different self (e.g. [self release]; return [[NSGregorianCalendar alloc] init]), and you don't want that. What calendar identifier are you passing it? Maybe it's complaining that it requires a "concrete implementation" that it knows about for the calendar identifier you're passing.


A direct inherit from NSCalendar is tricky because I think that it's a class cluster. I'm a bit fuzzy on this concept so I'll give you a link to the Apple document instead of making a greater fool of myself.

http://developer.apple.com/Mac/library/documentation/Cocoa/Conceptual/CocoaFundamentals/CocoaObjects/CocoaObjects.html#//apple_ref/doc/uid/TP40002974-CH4-SW34


Try using the NSTimer method

+ (NSTimer *)scheduledTimerWithTimeInterval:target:selector:userInfo:repeats:

Set the repeats to TRUE and have a time interval of 2 minutes. Have a counter that you set to 1 or 10. Everytime the selector is called you decrement the counter until it reaches 0 when you do your work and reset the counter.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜