メモメモ。
#import <EventKit/EventKit.h> #import "ViewController.h" @interface ViewController () @end @implementation ViewController - (void)viewDidLoad { [super viewDidLoad]; // Do any additional setup after loading the view, typically from a nib. EKEventStore *eventStore = [[EKEventStore alloc] init]; // weakつけるとブロックの中で参照がなくなってたので、ダメみたい。でもselfは大丈夫 // でもつけないとリークしちゃうよね? // __weak EKEventStore *__eventStore = eventStore; __weak ViewController *__self = self; [eventStore requestAccessToEntityType:EKEntityTypeEvent completion:^(BOOL granted, NSError *error) { // なので、ブロックの中で再生成してみた EKEventStore *__eventStore = [[EKEventStore alloc] init]; NSLog(@"__eventStore = %@", __eventStore.description); NSLog(@"__self = %@", __self.description); // カレンダーへのアクセスが許可された場合 // 一度拒否を選んでしまうと二度とアクセスできないっぽい // ユーザーに設定からリセットしてもらう必要がある // via http://d.hatena.ne.jp/appbakery/20130327/p2 // via http://program.station.ez-net.jp/special/handbook/objective-c/iphone/grant/reset.asp if (granted == NO) { // ここはmainキューでないので、UIの操作はdispatch_get_main_queueを使ってmainキューの中でやる必要がある // via http://d.hatena.ne.jp/zentoo/20121028/1351443185 dispatch_async(dispatch_get_main_queue(), ^{ UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"お知らせ" message:@"カレンダー操作の許可をくださいちょんまげ" delegate:self cancelButtonTitle:nil otherButtonTitles:@"OK", nil]; [alert show]; }); return; } //アクセス許可後の処理はここで EKEvent *event = [EKEvent eventWithEventStore:__eventStore]; event.title = @"ひささんピースDay"; event.timeZone = [NSTimeZone defaultTimeZone]; event.notes = @"ひささんを楽しませる会"; event.startDate = [[NSDate alloc] init]; event.endDate = [[NSDate alloc] initWithTimeInterval:6000 sinceDate:event.startDate]; event.calendar = __eventStore.defaultCalendarForNewEvents; [event setCalendar:[__eventStore defaultCalendarForNewEvents]]; NSError *err; BOOL result = [__eventStore saveEvent:event span:EKSpanThisEvent error:&err]; NSString *message = nil; if (result) { message = [NSString stringWithFormat:@"イベントの保存が完了しました。"]; } else { message = [NSString stringWithFormat:@"イベントの保存ができませんでした。 : %@", error]; } __weak NSString *__message = message; dispatch_async(dispatch_get_main_queue(), ^{ UIAlertView *alert = [[UIAlertView alloc] initWithTitle:nil message:__message delegate:nil cancelButtonTitle:nil otherButtonTitles:@"OK", nil]; [alert show]; }); }]; } - (void)didReceiveMemoryWarning { [super didReceiveMemoryWarning]; // Dispose of any resources that can be recreated. } @end
Objective-C超入門 改訂第2版――ゼロからしっかり学べるiPhoneプログラミング【Xcode 4.6で解説】
posted with amazlet at 13.06.13
大川内隆朗
ラトルズ
売り上げランキング: 107,707
ラトルズ
売り上げランキング: 107,707