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
NSDateobject,eventDate. You will want to set the date with time also. The reason you are getting that error is thatdateByAddingTimeInterval:should be called on an NSDate object and is not an identifier in itself. Set thefireDateof your local notification asThis will return a date which is 15 hours before the event.
EDIT: You need to create an
NSDateFormatterobject and set its format to how it is stored inmeetingDateField. Then use thedateFromString:to get theNSDatefrom the text field.