Add event reminder and event reminder functions

Source: Internet
Author: User

Add event reminder and event reminder functions

Add event reminder function

 

Effect

 

Source code

/// 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 = @ "do not die, do not die"; 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 = @ "Beijing"; event. delegate = self; [event save];}-(void) calendarEvent :( CalendarEvent *) event removedStatus :( ECalendarEventStatus) status error :( NSError *) error {if (status = kCalendarEventAccessSavedSucess) {NSLog (@ "saved successfully") ;}}@ 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]; N SError * 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? Authorization: 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] ;}// key value of the stored event [[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

 

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

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.