1. フレームワークを追加する。
・EventKit.framework
・EventKitUI.framework
2. ヘッダファイル定義
hogeViewController.h
#import <EventKit/EventKit.h>
#import <EventKitUI/EventKitUI.h>
@interface hogeViewController : UIViewController <EKEventEditViewDelegate>
{
EKEventStore *eventStore;
}
@property (nonatomic, retain) EKEventStore *eventStore;
@end
2. クラス定義
hogeViewController.m
#import "hogeViewController.h"
@implementation hogeViewController
@synthesize eventStore;
- (void)viewDidLoad {
[super viewDidLoad];
self.eventStore = [[EKEventStore alloc] init];
}
// eventEditViewControllerに渡す情報を編集し、eventEditViewControllerを起動するメソッド
- (void)eventAdd {
EKEvent *event = [EKEvent eventWithEventStore:self.eventStore];
event.title = @"Event Title"; // タイトル
event.location = @"Event Location"; // 場所
event.startDate = [NSDate date]; // 開始日
event.endDate = [NSDate date]; // 終了日
event.allDay = YES; // 終日
event.notes = @"Event Note"; // 備考
EKEventEditViewController *eventEditViewController = [[[EKEventEditViewController alloc] init] autorelease];
eventEditViewController.editViewDelegate = self;
eventEditViewController.event = event;
eventEditViewController.eventStore = self.eventStore;
eventEditViewController.modalPresentationStyle = UIModalPresentationFormSheet;
[self presentModalViewController:eventEditViewController animated:YES]; // eventEditViewControllerをmodalViewとして起動する
}
// eventEditViewControllerが閉じたときに呼ばれるメソッド
- (void)eventEditViewController:(EKEventEditViewController *)controller
didCompleteWithAction:(EKEventEditViewAction)action
{
NSError *error = nil;
switch (action) {
case EKEventEditViewActionCanceled:
break;
case EKEventEditViewActionSaved:
[controller.eventStore saveEvent:controller.event span:EKSpanThisEvent error:&error];
UIAlertView *alertView = [[UIAlertView alloc] initWithTitle:@"Infomation" message:@"Event Added on calendar" delegate:nil cancelButtonTitle:@"OK" otherButtonTitles: nil];
[alertView show];
[alertView release];
break;
case EKEventEditViewActionDeleted:
[controller.eventStore removeEvent:controller.event span:EKSpanThisEvent error:&error];
break;
default:
break;
}
[controller dismissModalViewControllerAnimated:YES];
}
【Eventkitの最新記事】