添加事件提醒功能,事件提醒功能

來源:互聯網
上載者:User

添加事件提醒功能,事件提醒功能

添加事件提醒功能

 

效果

 

源碼

////  ViewController.m//  Event////  Created by YouXianMing on 16/7/12.//  Copyright © 2016年 YouXianMing. All rights reserved.//#import "ViewController.h"#import "CalendarEvent.h"@interface ViewController () <CalendarEventDelegate>@end@implementation ViewController- (void)viewDidLoad {        [super viewDidLoad];        NSTimeInterval hour        = 60.f * 60.f;    NSDate        *currentDate = [NSDate date];        CalendarEvent *event = [CalendarEvent new];    event.eventTitle     = @"不作死就不會死";    event.startDate      = [NSDate dateWithTimeInterval:hour * 1 sinceDate:currentDate];    event.endDate        = [NSDate dateWithTimeInterval:hour * 2 sinceDate:currentDate];    event.alarmDate      = [NSDate dateWithTimeInterval:hour * 0.5f sinceDate:currentDate];    event.eventLocation  = @"大北京";    event.delegate       = self;    [event save];}- (void)calendarEvent:(CalendarEvent *)event removedStatus:(ECalendarEventStatus)status error:(NSError *)error {    if (status == kCalendarEventAccessSavedSucess) {                NSLog(@"儲存成功");    }}@end
////  CalendarEvent.h//  EventStore////  Created by YouXianMing on 16/7/8.//  Copyright © 2016年 XianMing You. All rights reserved.//#import <Foundation/Foundation.h>@class CalendarEvent;typedef enum : NSUInteger {        /**     *  Have not permission to save the event to system.     */    kCalendarEventAccessDenied = 1000,        /**     *  Saved the event success.     */    kCalendarEventAccessSavedSucess,        /**     *  Saved the event failed.     */    kCalendarEventAccessSavedFailed,        /**     *  Removed the event success.     */    kCalendarEventAccessRemovedSucess,        /**     *  Removed the event failed.     */    kCalendarEventAccessRemovedFailed,    } ECalendarEventStatus;@protocol CalendarEventDelegate <NSObject>@optional/** *  The CalendarEvent saved status. * *  @param event  CalendarEvent's object. *  @param status Saved status. *  @param error  Error infomation. */- (void)calendarEvent:(CalendarEvent *)event savedStatus:(ECalendarEventStatus)status error:(NSError *)error;/** *  The CalendarEvent removed status. * *  @param event  CalendarEvent's object. *  @param status Removed status. *  @param error  Error infomation. */- (void)calendarEvent:(CalendarEvent *)event removedStatus:(ECalendarEventStatus)status error:(NSError *)error;@end@interface CalendarEvent : NSObject/** *  Event title. */@property (nonatomic, strong) NSString *eventTitle;/** *  Alarm date. */@property (nonatomic, strong) NSDate   *alarmDate;/** *  Event start date. */@property (nonatomic, strong) NSDate   *startDate;/** *  Event end date. */@property (nonatomic, strong) NSDate   *endDate;/** *  Event location. */@property (nonatomic, strong) NSString *eventLocation;/** *  CalendarEvent's delegate. */@property (nonatomic, weak)   id <CalendarEventDelegate>  delegate;/** *  Save the event to system. */- (void)save;/** *  Remove the event. */- (void)remove;/** *  To indicate the event have saved or not. * *  @return */- (BOOL)haveSaved;#pragma mark - Constructor method.+ (instancetype)calendarEventWithEventTitle:(NSString *)title startDate:(NSDate *)startDate endDate:(NSDate *)endDate;@end
////  CalendarEvent.m//  EventStore////  Created by YouXianMing on 16/7/8.//  Copyright © 2016年 XianMing You. All rights reserved.//#import "CalendarEvent.h"#import <CommonCrypto/CommonDigest.h>#import <EventKit/EventKit.h>@implementation CalendarEvent- (void)remove {        NSString *eventId = [[NSUserDefaults standardUserDefaults] objectForKey:[self storedKey]];        if (eventId) {                EKEventStore *eventStore = [[EKEventStore alloc] init];        EKEvent      *event      = [eventStore eventWithIdentifier:eventId];        NSError      *error      = nil;        [eventStore removeEvent:event span:EKSpanThisEvent error:&error];                if (self.delegate && [self.delegate respondsToSelector:@selector(calendarEvent:removedStatus:error:)]) {                        dispatch_async(dispatch_get_main_queue(), ^{                                [self.delegate calendarEvent:self                               removedStatus:error ? kCalendarEventAccessRemovedFailed : kCalendarEventAccessRemovedSucess                                       error:nil];            });        }    }}- (BOOL)haveSaved {        NSString *eventId = [[NSUserDefaults standardUserDefaults] objectForKey:[self storedKey]];        if (eventId.length) {                return YES;            } else {                return NO;    }}- (void)save {        NSParameterAssert(self.eventTitle);    NSParameterAssert(self.startDate);    NSParameterAssert(self.endDate);        EKEventStore *eventStore = [[EKEventStore alloc] init];        [eventStore requestAccessToEntityType:EKEntityTypeEvent completion:^(BOOL granted, NSError *error) {                if (granted) {                        EKEvent *event  = [EKEvent eventWithEventStore:eventStore];            event.calendar  = [eventStore defaultCalendarForNewEvents];            event.title     = self.eventTitle;            event.startDate = self.startDate;            event.endDate   = self.endDate;                        self.alarmDate            ? [event addAlarm:[EKAlarm alarmWithAbsoluteDate:self.alarmDate]] : 0;            self.eventLocation.length ? event.location = self.eventLocation                             : 0;                        NSError *savedError = nil;            if ([eventStore saveEvent:event span:EKSpanThisEvent commit:YES error:&savedError]) {                                if (self.delegate && [self.delegate respondsToSelector:@selector(calendarEvent:savedStatus:error:)]) {                                        dispatch_async(dispatch_get_main_queue(), ^{                                                [self.delegate calendarEvent:self                                         savedStatus:kCalendarEventAccessSavedSucess                                               error:nil];                    });                }                                // 儲存事件的索引值                [[NSUserDefaults standardUserDefaults] setObject:event.eventIdentifier forKey:[self storedKey]];                            } else {                                if (self.delegate && [self.delegate respondsToSelector:@selector(calendarEvent:savedStatus:error:)]) {                                        dispatch_async(dispatch_get_main_queue(), ^{                                                [self.delegate calendarEvent:self                                         savedStatus:kCalendarEventAccessSavedFailed                                               error:savedError];                    });                }            }                    } else {                        if (self.delegate && [self.delegate respondsToSelector:@selector(calendarEvent:savedStatus:error:)]) {                                dispatch_async(dispatch_get_main_queue(), ^{                                        [self.delegate calendarEvent:self                                     savedStatus:kCalendarEventAccessDenied                                           error:error];                });            }        }    }];}- (NSString *)storedKey {        NSParameterAssert(self.eventTitle);    NSParameterAssert(self.startDate);    NSParameterAssert(self.endDate);        NSString *string = [NSString stringWithFormat:@"%@%@%@", self.eventTitle, self.startDate, self.endDate];        return [self md532BitLower:string];}- (NSString*)md532BitLower:(NSString *)string {        const char    *cStr = [string UTF8String];    unsigned char  result[16];    CC_MD5(cStr, (unsigned int)strlen(cStr), result);        return [[NSString stringWithFormat:@"%02X%02X%02X%02X%02X%02X%02X%02X%02X%02X%02X%02X%02X%02X%02X%02X",             result[0], result[1], result[2], result[3],             result[4], result[5], result[6], result[7],             result[8], result[9], result[10], result[11],             result[12], result[13], result[14], result[15]] lowercaseString];}+ (instancetype)calendarEventWithEventTitle:(NSString *)title startDate:(NSDate *)startDate endDate:(NSDate *)endDate {        CalendarEvent *event = [[self class] new];    event.eventTitle     = title;    event.startDate      = startDate;    event.endDate        = endDate;        return event;}@end

 

相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.