iOS新增事件到系統日曆
阿新 • • 發佈:2019-02-05
最近在專案中需要接觸在工程中接受到某個時間後將其解析後寫入iPhone系統自帶日曆中,在通過研究文件與資料後實現,特此記錄:
首先: 看下效果圖:
專案中呼叫到系統庫 EventKit.framework.
利用蘋果提供的介面完全可以實現此功能.下面貼上核心程式碼:
匯入標頭檔案
#import <EventKit/EventKit.h>
demo中是通過按鈕點選事件來執行寫入日曆操作.實際專案中一般通過監聽事件執行:
- (void)viewDidLoad {
[super viewDidLoad];
//新增點選按鈕
UIButton * button = [[UIButton alloc]init];
button.frame = CGRectMake(0, 0, 100, 40);
button.center = CGPointMake(self.view.frame.size.width*0.5 , self.view.frame.size.height * 0.5);
button.backgroundColor = [UIColor redColor];
[button setTitle:@"寫日曆" forState:UIControlStateNormal];
[button setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
[button addTarget:self action:@selector(saveEvent:) forControlEvents:UIControlEventTouchUpInside];
[self.view addSubview:button];
}
//點選事件處理
-(void)saveEvent:(id)sender
{
//calshow:後面加時間戳格式,也就是NSTimeInterval
// 注意這裡計算時間戳呼叫的方法是-
// NSTimeInterval nowTimestamp = [[NSDate date] timeIntervalSinceDate:2016];
// timeIntervalSinceReferenceDate的參考時間是2000年1月1日,
// [NSDate date]是你希望跳到的日期。
EKEventStore *eventStore = [[EKEventStore alloc] init];
//06.07 使用 requestAccessToEntityType:completion: 方法請求使用使用者的日曆資料庫
if ([eventStore respondsToSelector:@selector(requestAccessToEntityType:completion:)])
{
// the selector is available, so we must be on iOS 6 or newer
[eventStore requestAccessToEntityType:EKEntityTypeEvent completion:^(BOOL granted, NSError *error) {
dispatch_async(dispatch_get_main_queue(), ^{
if (error)
{
//錯誤細心
// display error message here
}
else if (!granted)
{
//被使用者拒絕,不允許訪問日曆
// display access denied error message here
}
else
{
// access granted
// ***** do the important stuff here *****
//事件儲存到日曆
//06.07 元素
//title(標題 NSString),
//location(位置NSString),
//startDate(開始時間 2016/06/07 11:14AM),
//endDate(結束時間 2016/06/07 11:14AM),
//addAlarm(提醒時間 2016/06/07 11:14AM),
//notes(備註類容NSString)
//建立事件
EKEvent *event = [EKEvent eventWithEventStore:eventStore];
event.title = @"測試寫入日曆事件";
event.location = @"北京海淀";
// NSDateFormatter *tempFormatter = [[NSDateFormatter alloc]init];
// [tempFormatter setDateFormat:@"dd.MM.yyyy HH:mm"];
//06.07 時間格式
NSDateFormatter * dateFormatter = [[NSDateFormatter alloc] init];
[dateFormatter setAMSymbol:@"AM"];
[dateFormatter setPMSymbol:@"PM"];
[dateFormatter setDateFormat:@"yyyy/MM/dd hh:mmaaa"];
NSDate *date = [NSDate date];
NSString * s = [dateFormatter stringFromDate:date];
NSLog(@"%@",s);
//開始時間(必須傳)
event.startDate = [date dateByAddingTimeInterval:60 * 2];
//結束時間(必須傳)
event.endDate = [date dateByAddingTimeInterval:60 * 5 * 24];
// event.endDate = [[NSDate alloc]init];
// event.allDay = YES;//全天
//新增提醒
//第一次提醒 (幾分鐘後)
[event addAlarm:[EKAlarm alarmWithRelativeOffset:60.0f * -1.0f]];
//第二次提醒 ()
// [event addAlarm:[EKAlarm alarmWithRelativeOffset:60.0f * -10.0f * 24]];
//06.07 add 事件類容備註
NSString * str = @"接受資訊類容備註";
event.notes = [NSString stringWithFormat:@"%@",str];
[event setCalendar:[eventStore defaultCalendarForNewEvents]];
NSError *err;
[eventStore saveEvent:event span:EKSpanThisEvent error:&err];
NSLog(@"儲存成功");
//直接殺死程序
exit(2);
}
});
}];
}
// [[UIApplication sharedApplication] openURL:[NSURL URLWithString:@"calshow:"]];
}
在點選執行後通過exit(2)直接殺死程序.所以此時去日曆中就可以看到剛才的事件寫入成功了.
下面是一些官方文件有興趣可以看下: