隨著App的開發(fā)需求,很多軟件有用戶發(fā)布信息的需求,該功能也許是針對用戶自己的需求而發(fā)布的、也許是他人。反正就是針對事件將在未來的某個時間發(fā)生而做的預(yù)先處理。比如:參加組織某個活動,舉辦某個活動的等等。那么本文章就是針對作者所預(yù)先設(shè)定即將發(fā)生的事情添加到手機(jī)系統(tǒng)日歷提醒事項,并設(shè)定需要提示的時間。
其實與大家共勉這個也是自己的項目需求,然后就...與大家共勉,不喜勿噴,多多指教。
系統(tǒng)日歷提醒事項的幾個屬性:
1、事件標(biāo)題
2、事件發(fā)生的位置位置
3、開始時間
4、結(jié)束時間
5、是否全天
6、事件發(fā)生前提醒的鬧鐘設(shè)定
項目需求:用戶發(fā)布未來的時間將會發(fā)生的活動、事情。發(fā)布成功直接將此事件添加到手機(jī)系統(tǒng)日歷提醒事項,添加成功繼續(xù)后面的一系列邏輯處理...
下面直接上代碼...封裝的...
下面是項目抽出來的類,當(dāng)然做了一些修改,保證You可以直接使用:
#import <Foundation/Foundation.h>
@interface EventCalendar : NSObject
+ (instancetype)sharedEventCalendar;
/**
* 將App事件添加到系統(tǒng)日歷提醒事項,實現(xiàn)鬧鈴提醒的功能
*
* @param title 事件標(biāo)題
* @param location 事件位置
* @param startDate 開始時間
* @param endDate 結(jié)束時間
* @param allDay 是否全天
* @param alarmArray 鬧鐘集合
* @param block 回調(diào)方法
*/
- (void)createEventCalendarTitle:(NSString *)title location:(NSString *)location startDate:(NSDate *)startDate endDate:(NSDate *)endDate allDay:(BOOL)allDay alarmArray:(NSArray *)alarmArray;
@end
#import "EventCalendar.h"
#import <EventKit/EventKit.h>
#import <UIKit/UIKit.h>
@implementation EventCalendar
static EventCalendar *calendar;
+ (instancetype)sharedEventCalendar{
static dispatch_once_t onceToken;
dispatch_once(&onceToken, ^{
calendar = [[EventCalendar alloc] init];
});
return calendar;
}
+(instancetype)allocWithZone:(struct _NSZone *)zone{
static dispatch_once_t onceToken;
dispatch_once(&onceToken, ^{
calendar = [super allocWithZone:zone];
});
return calendar;
}
- (void)createEventCalendarTitle:(NSString *)title location:(NSString *)location startDate:(NSDate *)startDate endDate:(NSDate *)endDate allDay:(BOOL)allDay alarmArray:(NSArray *)alarmArray{
__weak typeof(self) weakSelf = self;
EKEventStore *eventStore = [[EKEventStore alloc] init];
if ([eventStore respondsToSelector:@selector(requestAccessToEntityType:completion:)])
{
[eventStore requestAccessToEntityType:EKEntityTypeEvent completion:^(BOOL granted, NSError *error){
dispatch_async(dispatch_get_main_queue(), ^{
__strong typeof(weakSelf) strongSelf = weakSelf;
if (error)
{
[strongSelf showAlert:@"添加失敗,請稍后重試"];
}else if (!granted){
[strongSelf showAlert:@"不允許使用日歷,請在設(shè)置中允許此App使用日歷"];
}else{
EKEvent *event = [EKEvent eventWithEventStore:eventStore];
event.title = title;
event.location = location;
NSDateFormatter *tempFormatter = [[NSDateFormatter alloc]init];
[tempFormatter setDateFormat:@"dd.MM.yyyy HH:mm"];
event.startDate = startDate;
event.endDate = endDate;
event.allDay = allDay;
//添加提醒
if (alarmArray && alarmArray.count > 0) {
for (NSString *timeString in alarmArray) {
[event addAlarm:[EKAlarm alarmWithRelativeOffset:[timeString integerValue]]];
}
}
[event setCalendar:[eventStore defaultCalendarForNewEvents]];
NSError *err;
[eventStore saveEvent:event span:EKSpanThisEvent error:&err];
[strongSelf showAlert:@"已添加到系統(tǒng)日歷中"];
}
});
}];
}
}
- (void)showAlert:(NSString *)message
{
UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"提示" message:message delegate:self cancelButtonTitle:@"確定" otherButtonTitles:nil, nil];
[alert show];
}
@end
等等再調(diào)用的時候,如果需要在時間發(fā)生前提醒用戶,需要傳入提醒時間數(shù)組,這個數(shù)組是按照秒計算的。比如時間發(fā)生前一天提醒,時間數(shù)組的元素就是:-86400
希望本文章對需要此功能的朋友有幫助,歡迎多多指教,本菜鳥將陸續(xù)分享自己遇到常用知識點的封裝與大家共勉,歡迎多多指教,不喜勿噴。