iOS不同时间的多个警报

时间:2014-01-28 12:18:57

标签: ios objective-c uilocalnotification ekevent

我需要创建一天4个闹钟,精确日出和日落时间以及提醒时间前15分钟。

警报应显示有声音的警报正文。

我来日出和日落的时间来自网络服务。我需要在没有任何用户输入的情况下获取这些值后直接设置警报。

这是我的问题: - >如何在一天内为不同的时间设置多个警报???

我已经尝试了几乎所有可能的方法,但无法通过它。

有本地通知:

NSString *formattedStringForAm = [NSString stringWithFormat:@"%@ %@", dateUnformatted, riseTime]; // dateUnformatted is the date for which I'm setting the alarm
NSString *formattedStringForPm = [NSString stringWithFormat:@"%@ %@", dateUnformatted, setTime];

NSDateFormatter *dateFormatterr = [[NSDateFormatter alloc]init];
//[dateFormatterr setTimeZone:[NSTimeZone timeZoneForSecondsFromGMT:0]];
[dateFormatterr setTimeZone:[NSTimeZone defaultTimeZone]];
[dateFormatterr setDateFormat:@"h:mm a"];

NSDate *dateCheckForAM = [dateFormatterr dateFromString:formattedStringForAm];
NSDate *dateCheckForPM = [dateFormatterr dateFromString:formattedStringForPm];

NSLog(@"dateCheckForPM =  %@ and dateCheckForAM = %@",dateCheckForPM, dateCheckForAM);

// Schedule the notification
UILocalNotification* localNotification = [[UILocalNotification alloc] init];
localNotification.fireDate = dateCheckForAM;
localNotification.alertBody = @"Time To Perform Agnihotra !!!";
localNotification.alertAction = @"Show me the item";
localNotification.soundName = [NSString stringWithFormat:@"Eas Beep-SoundBible.mp3"];
localNotification.applicationIconBadgeNumber = [[UIApplication sharedApplication] applicationIconBadgeNumber] + 1;

[[UIApplication sharedApplication] scheduleLocalNotification:localNotification];

与EKEVENT和EKALARM:

EKEventStore *eventStore = [[EKEventStore alloc] init];
if([eventStore respondsToSelector:@selector(requestAccessToEntityType:completion:)]) {
    // iOS 6 and later
    [eventStore requestAccessToEntityType:EKEntityTypeEvent completion:^(BOOL granted, NSError *error) {
        if (granted){
        //---- codes here when user allow your app to access theirs' calendar.

                 EKReminder *reminder = [EKReminder reminderWithEventStore:_eventStore];
                 reminder.title = [NSString stringWithFormat:@""];
                 reminder.calendar = [_eventStore defaultCalendarForNewReminders];

                 EKAlarm *alarm = [EKAlarm alarmWithAbsoluteDate:dateCheckForAM];

                 [reminder addAlarm:alarm];
            }else
            {
                UIAlertView *alert = [[UIAlertView alloc]initWithTitle:@"Error !" message:@"Alarm cannot set without access to Calendar..." delegate:nil cancelButtonTitle:@"OK" otherButtonTitles:nil, nil];
                [alert show];
                //----- codes here when user NOT allow your app to access the calendar.
            }
        }];
    }
    else {
        //---- codes here for IOS < 6.0.
    }
}

以下的多个警报的代码:

NSMutableArray *myAlarmsArray = [[NSMutableArray alloc] init];

EKAlarm *alarm1 = [EKAlarm alarmWithAbsoluteDate:dateCheckForAM]; // AM Time Alarm
EKAlarm *alarm2 = [EKAlarm alarmWithAbsoluteDate:dateCheckForPM]; // PM Time Alarm

[myAlarmsArray addObject:alarm1];
[myAlarmsArray addObject:alarm2];

_eventStore.alarms = myAlarmsArray;

但仍然无法根据需要设置闹钟。

0 个答案:

没有答案