iphone 定时提醒

//定时提醒
-(void)schedulNotificationWithYear:(int)y andMonth:(int)m andDay:(int)d andHour:(int)h andMintu:(int)mi andMsg:(NSString *)msg{
    
    // Set up the fire time
    NSCalendar *calendar = [NSCalendar autoupdatingCurrentCalendar];
    NSDateComponents *dateComps = [[NSDateComponents alloc] init];
    [dateComps setDay:d];
    [dateComps setMonth:m];
    [dateComps setYear:y];
    [dateComps setHour:h];
// Notification will fire in one minute
    [dateComps setMinute:mi];
[dateComps setSecond:0];
    NSDate *itemDate = [calendar dateFromComponents:dateComps];
    [dateComps release];
    
    UILocalNotification *localNotif = [[UILocalNotification alloc] init];
    if (localNotif == nil)
        return;
    localNotif.fireDate = itemDate;
    localNotif.timeZone = [NSTimeZone defaultTimeZone];
    
// Notification details
    localNotif.alertBody = msg;
// Set the action button
    localNotif.alertAction = @"查看";
    
    localNotif.soundName = UILocalNotificationDefaultSoundName;
    //    localNotif.applicationIconBadgeNumber = 1;
    
// Specify custom data for the notification
    NSDictionary *infoDict = [NSDictionary dictionaryWithObject:@"someValue" forKey:@"someKey"];
    localNotif.userInfo = infoDict;
    
// Schedule the notification
    [[UIApplication sharedApplication] scheduleLocalNotification:localNotif];
    [localNotif release];
}

在AppDelegate.m中添加如下代码,相当于回调函数


- (void)application:(UIApplication *)application didReceiveLocalNotification:(UILocalNotification*)notif {

    UITextView *tv = (UITextView *)[[application keyWindow] viewWithTag:11];
    NSString *status = [NSString stringWithFormat:@"%@", [notif description]];
    tv.text = status;
    //添加处理代码
}
原文地址:https://www.cnblogs.com/lm3515/p/2546101.html