在iOS中制作警报应用程序

时间:2016-02-22 07:46:00

标签: ios uilocalnotification alarm

我正在制作警报应用程序。当我设置闹钟时,通知会立即触发。我为警报设置了两个计划通知功能。一个是每天重复报警,另一个是每周重复报警。我的日常重复警报代码工作正常但是每周重复警报,通知会立即触发,有时会在不同的日期触发。 这是每周重复警报的代码。

-(void) scheduleLocalNotificationWithWeek:(NSDate *)fireDate
{

    UILocalNotification *localNotif = [[UILocalNotification alloc] init];

    localNotif.fireDate = calculateAlarmDay;
    NSLog(@"%@",localNotif.fireDate);
    localNotif.timeZone = [NSTimeZone localTimeZone];
    localNotif.alertBody = @"Time to wake Up";
    localNotif.alertAction = @"Show me";
    localNotif.soundName = @"Tick-tock-sound.mp3";
    localNotif.applicationIconBadgeNumber = 1;
    localNotif.repeatInterval = NSWeekCalendarUnit;
    [[UIApplication sharedApplication] scheduleLocalNotification:localNotif];


}

这里是一个代码,我在其中计算它发射的日期,例如,如果用户选择星期日而不是星期日28-2-2016和下一个星期日6-3-2016等等......

-(void) repeataftersevendays:(int) day
{
    NSCalendar *calendar = [NSCalendar currentCalendar];
    NSDateComponents *components = [[NSCalendar currentCalendar] components:NSCalendarUnitDay | NSCalendarUnitMonth | NSCalendarUnitYear fromDate:[NSDate date]];

    NSLog(@"%@",components);
    [components setTimeZone:[NSTimeZone localTimeZone]];
    [calendar setTimeZone: [NSTimeZone localTimeZone]];

    finalValue = 0;

    if (weekday == day)
    {
        finalValue=0;
    }
    else if (weekday < day)
    {
        int tempWeekDay = weekday;
        while ( tempWeekDay  < day )
        {
            tempWeekDay++;
            finalValue++;
            NSLog(@"%i",finalValue);
        }
    }
    else
    {
        int tempDay = day;
        while ( weekday  >= tempDay )
        {
            tempDay++;
            finalValue++;
        }
    }

    calculateAlarmDay = [calendar dateByAddingUnit:NSCalendarUnitDay
                                       value:finalValue
                                      toDate:[NSDate date]
                                          options:0];;
    NSLog(@"value is %@",calculateAlarmDay);

}

我在过去七天里坚持使用这些东西。请告诉我解决方案。感谢

0 个答案:

没有答案