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 に答える