話不多說直接來
一吏奸、權(quán)限的獲取
Privacy - Calendars Usage Description 是否允許此App使用日歷蕴纳?
Privacy - Reminders Usage Description 是否允許此App訪問提醒事項(xiàng)胸私?
更多權(quán)限獲确踩恕:http://www.reibang.com/p/31ef0f8ba34e
二、手動(dòng)寫入日歷日程-代碼部分
// 導(dǎo)入EventKit 庫(kù)
#import <EventKit/EventKit.h>
// 先來一波權(quán)限判斷
- (void)saveCalendar {
EKEventStore *eventStore = [[EKEventStore alloc] init];
__weak typeof(self) weakSelf = self;
//06.07 使用 requestAccessToEntityType:completion: 方法請(qǐng)求使用用戶的日歷數(shù)據(jù)庫(kù)
if ([eventStore respondsToSelector:@selector(requestAccessToEntityType:completion:)]) {
// 獲取訪問權(quán)限
[eventStore requestAccessToEntityType:EKEntityTypeEvent completion:^(BOOL granted, NSError *error) {
if (error)
{
//報(bào)錯(cuò)啦
}
else if (!granted)
{
// 被用戶拒絕嘹朗,不允許訪問日歷蔬啡,滾去開啟權(quán)限
}
else
{
dispatch_async(dispatch_get_main_queue(), ^{
// 用戶既然允許事件保存到日歷诲侮,那就去保存吧
[weakSelf saveDataCalendar:eventStore];
});
}
}];
}
}
// 寫入日歷
- (void)saveDataCalendar:(EKEventStore *)eventStore {
// 創(chuàng)建事件
EKEvent *event = [EKEvent eventWithEventStore:eventStore];
// 這里拿當(dāng)前時(shí)間 (自己寫的話格式最好用 yyyy/MM/dd hh:mmaaa)
NSDate *date = [NSDate date];
event.title = @"事件標(biāo)題標(biāo)標(biāo)標(biāo)" ; // 事件標(biāo)題
event.location = @"事件地點(diǎn)地地地地" ; // 事件地點(diǎn)
event.notes = @"事件備注備備備"; // 事件備注
event.startDate = [date dateByAddingTimeInterval:60 * 2]; // 開始時(shí)間
event.endDate = [date dateByAddingTimeInterval:60 * 30]; // 結(jié)束時(shí)間
//第一次提醒 (幾分鐘后)
[event addAlarm:[EKAlarm alarmWithRelativeOffset:60.0f * -5.0f]];
[event setCalendar:[eventStore defaultCalendarForNewEvents]];
NSError *err;
[eventStore saveEvent:event span:EKSpanThisEvent error:&err];
}
三、利用系統(tǒng)界面寫入日歷日程-代碼部分
// 導(dǎo)入EventKit和EventKitUI 庫(kù)
#import <EventKit/EventKit.h>
#import <EventKitUI/EventKitUI.h>
- (void) saveCalendar {
EKEventStore *eventStore = [[EKEventStore alloc]init];
if([eventStore respondsToSelector:@selector(requestAccessToEntityType:completion:)]) {
[eventStore requestAccessToEntityType:EKEntityTypeEvent completion:^(BOOL granted,NSError* error){
if(!granted){
dispatch_async(dispatch_get_main_queue(), ^{
//TODO: 提示需要權(quán)限
});
}else{
EKEvent *event = [EKEvent eventWithEventStore:eventStore];
event.title = @"日歷標(biāo)題";
EKCalendar* calendar;
calendar = [EKCalendar calendarForEntityType:EKEntityTypeEvent eventStore:eventStore];
NSError* error;
[eventStore saveCalendar:calendar commit:YES error:&error];
EKEventEditViewController *addController = [[EKEventEditViewController alloc] initWithNibName:nil bundle:nil];
addController.event = event;
addController.eventStore = eventStore;
[self presentViewController:addController animated:YES completion:nil];
addController.editViewDelegate = self;
}
}];
}
}
#pragma mark - eventEditDelegates -
- (void)eventEditViewController:(EKEventEditViewController *)controller didCompleteWithAction:(EKEventEditViewAction)action{
if (action ==EKEventEditViewActionCanceled) {
[self dismissViewControllerAnimated:YES completion:nil];
}
if (action==EKEventEditViewActionSaved) {
[self dismissViewControllerAnimated:YES completion:nil];
}
}