我需要在我的应用程序中设置闹钟并通知我尝试了以下代码,但没有运气给我
EKEventStore * eventStore1 = [[EKEventStore alloc] init];
if([eventStore1 respondsToSelector:@selector(requestAccessToEntityType:completion:)]) {
[eventStore1 requestAccessToEntityType:EKEntityTypeEvent completion:^(BOOL granted, NSError *error) {
// This code will run when uses has made his/her choice
EKEvent *event = [EKEvent eventWithEventStore:eventStore1];
NSDate * date = [NSDate date];
NSLog(@"%@",date);
event.title = @"HAIManiiiiii";
event.location = @" ";
event.startDate = date;
event.endDate = [[[NSDate alloc] initWithTimeInterval:36 sinceDate:event.startDate] autorelease];
EKRecurrenceRule *rule = [[EKRecurrenceRule alloc]
initRecurrenceWithFrequency:EKRecurrenceFrequencyYearly
interval:1
end:[EKRecurrenceEnd recurrenceEndWithOccurrenceCount:10]];
EKAlarm *alarm1 = [EKAlarm alarmWithRelativeOffset:-36];
NSArray * ruleArray = [[NSArray alloc]initWithObjects:rule, nil];
NSArray * alrmArray = [[NSArray alloc] initWithObjects: alarm1, nil];
event.alarms = alrmArray;
event.recurrenceRules = ruleArray;
[event setCalendar:[eventStore1 defaultCalendarForNewEvents]];
NSError *err;
// [eventStore1 saveEvent:event span:EKSpanThisEvent error:&err];
NSLog(@"%d",[eventStore1 saveEvent:event span:EKSpanThisEvent error:&err]);
NSLog(@"Error From iCal : %@", [err description]);
}]
该活动正在添加到日历中,因为它不适合我。请建议我解决此问题的一些想法
答案 0 :(得分:2)
使用以下代码。它可能对我很好。
EKEventStore *eventStore = [[[EKEventStore alloc] init] autorelease];
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.
EKEventStore *eventStore = [[EKEventStore alloc] init];
EKEvent *event = [EKEvent eventWithEventStore:eventStore];
event.title = @"My Event";
event.notes =@"";
NSLog(@"%@",[StartTimePicker date]);
event.startDate = [StartTimePicker date];
// event.startDate = delegate.SelectedDate;
event.endDate = [[NSDate alloc] initWithTimeInterval:600 sinceDate:event.startDate];
[event setCalendar:[eventStore defaultCalendarForNewEvents]];
NSError *err;
EKAlarm *alarm=[EKAlarm alarmWithRelativeOffset:600];
[event addAlarm:alarm];
BOOL Save= [eventStore saveEvent:event span:EKSpanThisEvent error:&err];
NSLog(@"%d",Save);
if (Save) {
UIAlertView *alert=[[UIAlertView alloc]initWithTitle:@"Success" message:@"Alarm set" delegate:self cancelButtonTitle:@"Ok" otherButtonTitles:nil, nil];
[alert show];
[alert release];
[DejalBezelActivityView removeViewAnimated:YES];
}
}else
{
[DejalBezelActivityView removeViewAnimated:YES];
//----- codes here when user NOT allow your app to access the calendar.
NSLog(@"Permission not allowed");
UIAlertView *alert=[[UIAlertView alloc]initWithTitle:@"Permissions not allowed" message:@"Please allow access permission in Calendar setting" delegate:self cancelButtonTitle:@"OK" otherButtonTitles:nil, nil];
[alert show];
[alert release];
}
}];
}
else {
//---- codes here for IOS < 6.0.
EKEventStore *eventStore = [[EKEventStore alloc] init];
EKEvent *event = [EKEvent eventWithEventStore:eventStore];
event.title = @"My Event";
event.startDate =dt;
event.endDate = [[NSDate alloc] initWithTimeInterval:600 sinceDate:event.startDate];
[event setCalendar:[eventStore defaultCalendarForNewEvents]];
NSError *err;
EKAlarm *alarm=[EKAlarm alarmWithRelativeOffset:600];
[event addAlarm:alarm];
BOOL Save= [eventStore saveEvent:event span:EKSpanThisEvent error:&err];
NSLog(@"%d",Save);
if (Save) {
UIAlertView *alert=[[UIAlertView alloc]initWithTitle:@"Success" message:@"Alarm set" delegate:self cancelButtonTitle:@"Ok" otherButtonTitles:nil, nil];
[alert show];
[alert release];
}
}