iphone - 如何在 iphone 中设置选定日期的闹钟?

标签 iphone ios nsnotificationcenter nsnotifications

我想为一周中选定的几天设置闹钟。

它可能是工作日(周一、周二、周三、周四或周五)、周末(周六、周日)或每天。

我如何使用本地通知来做到这一点?

最佳答案

如果你有时间必须每天触发通知,你应该这样做

NSCalendar *calendar = [[NSCalendar alloc] initWithCalendarIdentifier:NSGregorianCalendar];
    NSDateComponents *components = [[NSDateComponents alloc] init];
    for (NSMutableArray * arrDay in self.namaztimes) {
        NSLog(@"Alarm Array: %@", arrDay);
        int count=[arrDay count];
        if(!count){
            continue;
        }

        int day =0;
        int month=0;
        int year=0;
        int hour =0;
        int minutes=0;

        //  NSArray *arrDates=[[NSMutableArray alloc] initWithCapacity:1];
        for ( int i=0;i<3;i++) {
            NSString * dayTime=[arrDay objectAtIndex:i ];
            if (i==0) {
                day = [dayTime intValue];    
            }else if(i==1){
                month = [dayTime intValue];                    
            }else if(i==2){
                year = [dayTime intValue];    

            }
        }
        for ( int i=3;i<count;i++) {
            NSString * dayTime=[arrDay objectAtIndex:i ];
            hour = [[dayTime substringToIndex:2] intValue];
            minutes = [[dayTime substringFromIndex:3] intValue];

            [components setDay:day];
            [components setMonth:month];
            [components setYear:year];
            [components setMinute:minutes];
            [components setHour:hour];


            NSDate *myNewDate = [calendar dateFromComponents:components];

            [self scheduleNotificationForDate:myNewDate];

        }
    }

    [components release];
    [calendar release];

然后从这里连接到主要的通知触发方法
[self scheduleNotificationForDate:myNewDate];

-(void) scheduleNotificationForDate: (NSDate*)date {
    /* Here we cancel all previously scheduled notifications */
    [[UIApplication sharedApplication] cancelAllLocalNotifications];
    UILocalNotification *localNotification = [[UILocalNotification alloc] init];
    localNotification.fireDate = date;
    NSLog(@"Notification will be shown on: %@ ",localNotification.fireDate);

    localNotification.timeZone = [NSTimeZone defaultTimeZone];
    localNotification.alertBody = @"Your Notification Text"; //[NSString stringWithFormat:@"%@",date];
    localNotification.alertAction = NSLocalizedString(@"View details", nil);

    /* Here we set notification sound and badge on the app's icon "-1" 
     means that number indicator on the badge will be decreased by one 
     - so there will be no badge on the icon */

    localNotification.repeatInterval = NSDayCalendarUnit;
    localNotification.soundName = UILocalNotificationDefaultSoundName;
    localNotification.applicationIconBadgeNumber = -1;

    [[UIApplication sharedApplication] scheduleLocalNotification:localNotification];
}

关于iphone - 如何在 iphone 中设置选定日期的闹钟?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/10780517/

相关文章:

iphone - 在 iPhone 应用程序更新上将内容更新/插入到现有 Sqlite 数据库中

ios - 等待 UITextView 完成更新文本 (iOS 7.1)

objective-c - NSMetaDataQuery 从不使用 NSMetadataQueryDidFinishGatheringNotification 回调

ios - 对音量按钮没有响应 - objective-c

ios - NSLocalizedString 优缺点不加评论

ios - 在 SwiftUI 中动态创建列表部分

ios - 等待 Testflight 上的 Beta App Review

ios - 为什么我在不同的设备上得到不同的 CGFloat 值

iphone - iTunes 图稿文件的格式是什么?

javascript - WKWebView线程1 : signal SIGABRT when using location