0

我需要在我的应用程序中设置带有通知的警报我尝试了以下代码但我没有运气

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]);
 }]

该事件并通过它对我不起作用而添加到日历中。请建议我一些解决此问题的想法

4

1 回答 1

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];
        }
    }
于 2013-07-10T04:43:07.303 回答