I wrote an app that allows the user to enter data in a UITextField, and one of them allows them to enter in a specific date. I'm trying to have an alert appear in the iPhones Notification Center when the date is 15 hours away, even when the app is not running at all.
EDIT: New code-
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{ UILocalNotification *localNotif = [[UILocalNotification alloc] init];
NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
[dateFormatter setDateFormat:@"MMdd"];
NSDate *eventDate=[dateFormatter dateFromString:eventDateField.text];
localNotif.fireDate = [eventDate dateByAddingTimeInterval:-15*60*60];
localNotif.timeZone = [NSTimeZone defaultTimeZone];
localNotif.alertBody = @"Event Tomorrow!";
localNotif.alertAction = @"Show me";
localNotif.soundName = UILocalNotificationDefaultSoundName;
localNotif.applicationIconBadgeNumber = 0;
[[UIApplication sharedApplication]presentLocalNotificationNow:localNotif];
}
Store the date obtained from the text field in a NSDate object, eventDate. You will want to set the date with time also. The reason you are getting that error is that dateByAddingTimeInterval: should be called on an NSDate object and is not an identifier in itself. Set the fireDate of your local notification as
ReplyDeletelocalNotif.fireDate=[eventDate dateByAddingTimeInterval:-15*60*60];
This will return a date which is 15 hours before the event.
EDIT: You need to create an NSDateFormatter object and set its format to how it is stored in meetingDateField. Then use the dateFromString: to get the NSDate from the text field.
NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
[dateFormatter setDateFormat:@"mm'/'dd'/'yyyy"];
NSDate *eventDate=[dateFormatter dateFromString:meetingDateField.text];
Youre trying to send the message dateByAddingTimeInterval: to whom? Nobody. You need a receiver for the message, an object that can then run the method.
ReplyDelete[[NSDate date] dateByAddingTimeInterval: -20*60];
(NSDate date is the current date and time).
In order to call [-dateByAddingTimeInterval:], you must have an NSDate object. In your code above, you don't. It should look something like:
ReplyDeleteNSDate* now = [NSDate date];
localNotif.fireDate = [now dateByAddingTimeInterval:20*60];