我開發了一個應用程序,我的應用程序需要在設備的日曆上創建一個事件。我一直在通過EventKitProgGuide並研究了SimpleEKDemo。如何將事件標題(從UI文本視圖)添加到由我的APP創建的日曆事件?
通過簡化SimpleEKDemo中的代碼,我生成了下面顯示的代碼,它直接從我的應用程序打開'日曆的事件屏幕'並正確生成事件。我很確定。
現在我需要使用UITextView的文本內容作爲事件標題!
有人可以幫助我與該代碼?
感謝,
馬科斯
這裏是我的代碼:
@.h
#import <EventKitUI/EventKitUI.h>
#import <EventKit/EventKit.h>
@property (nonatomic, strong) EKEventStore *eventStore;
@property (nonatomic, strong) EKEvent *event;
@property (nonatomic, strong) EKCalendar *defaultCalendar;
@property (nonatomic, strong)
IBOutlet UITextView *textView1;
- (IBAction)agendar:(UIButton *)sender;
@.m
- (void)viewDidLoad
{
[super viewDidLoad];
self.eventStore = [[EKEventStore alloc]init];
self.textView1.text = @"hello world!";
}
-(void)viewDidAppear:(BOOL)animated
{
[super viewDidAppear:animated];
[self checkEventStoreAccessForCalendar];
}
- (IBAction)agendar:(UIButton *)sender {
EKEventEditViewController *addController = [[EKEventEditViewController alloc] init];
addController.eventStore = self.eventStore;
addController.editViewDelegate = self;
[self presentViewController:addController animated:YES completion:nil];
self.event = [EKEvent eventWithEventStore:self.eventStore];
// Jeff's suggested code:
self.event.title = self.textView1.text;
// Jeff's SaveEvent Sugestion
NSError *err;
[self.eventStore saveEvent:self.event span:EKSpanThisEvent error:&err];
}
-(void)checkEventStoreAccessForCalendar
{
EKAuthorizationStatus status = [EKEventStore authorizationStatusForEntityType:EKEntityTypeEvent];
switch (status)
{
case EKAuthorizationStatusAuthorized: [self accessGrantedForCalendar];
break;
case EKAuthorizationStatusNotDetermined: [self requestCalendarAccess];
break;
case EKAuthorizationStatusDenied:
case EKAuthorizationStatusRestricted:
{
UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"Alerta de Privacidade" message:@"Permissão de acesso ao calendário não concedida."
delegate:nil
cancelButtonTitle:@"OK"
otherButtonTitles:nil];
[alert show];
}
break;
default:
break;
}
}
-(void)requestCalendarAccess
{
[self.eventStore requestAccessToEntityType:EKEntityTypeEvent completion:^(BOOL granted, NSError *error)
{
if (granted)
{
Tela8ViewController * weakSelf = self;
dispatch_async(dispatch_get_main_queue(), ^{
[weakSelf accessGrantedForCalendar];
});
}
}];
}
-(void)accessGrantedForCalendar
{
self.defaultCalendar = self.eventStore.defaultCalendarForNewEvents;
}
- (void)eventEditViewController:(EKEventEditViewController *)controller
didCompleteWithAction:(EKEventEditViewAction)action
{
[self dismissViewControllerAnimated:YES completion:^
{
if (action != EKEventEditViewActionCanceled)
{
dispatch_async(dispatch_get_main_queue(), ^{
});
}
}];
}
我剛剛試過:self.event.title = self.textView1.text; NSLog(@「%@」,self.event.title)日誌結果是正確的,但是當日歷界面打開時,我會用通常的「標題」佔位符創建一個乾淨的「標題」字段......我需要這個佔位符textView文本...謝謝 –
@ mkta123:更改標題後,您是否通過在您的EKEventStore上調用-saveEvent來保存事件? –
不,還沒有。我已經用你的建議編輯了上面的代碼,所以你可以看到我添加它的位置「 - (IBAction)agendar:」。我不確定,但我明白,如果我從應用程序生成事件,則可以使用-saveEvent。在這種情況下,我從應用程序打開日曆事件屏幕,以便用戶可以設置日期,位置,鬧鐘等。除了標題之外的所有內容都應該來自textView。 -saveEvent適用於這種情況嗎? (同時我會嘗試構建一個代碼並試一試)。謝謝 –