2016-04-13 7 views

答えて

2

オーケーこの

// Get the appropriate calendar 
NSCalendar *calendar = [NSCalendar currentCalendar]; 

// Create the start date components 
NSDateComponents *oneDayAgoComponents = [[NSDateComponents alloc] init]; 
oneDayAgoComponents.day = -1; 
NSDate *oneDayAgo = [calendar dateByAddingComponents:oneDayAgoComponents 
               toDate:[NSDate date] 
              options:0]; 

// Create the end date components 
NSDateComponents *oneYearFromNowComponents = [[NSDateComponents alloc] init]; 
oneYearFromNowComponents.year = 1; 
NSDate *oneYearFromNow = [calendar dateByAddingComponents:oneYearFromNowComponents 
                toDate:[NSDate date] 
                options:0]; 

// Create the predicate from the event store's instance method 
NSPredicate *predicate = [store predicateForEventsWithStartDate:oneDayAgo 
                 endDate:oneYearFromNow 
                 calendars:nil]; 

// Fetch all events that match the predicate 
NSArray *events = [store eventsMatchingPredicate:predicate]; 
+0

https://developer.apple.com/library/ios/documentation/DataManagement/Conceptual/EventKitProgGuide/ReadingAndWritingEvents.html –

+0

をお試しくださいこれは私が必要としたものです。ありがとう。 –

0

Swift3.0バージョン

Get the user permission 

    func requestAccessToCalendar() { 
      eventStore.requestAccess(to: EKEntityType.event, completion: { 
       (accessGranted: Bool, error: Error?) in 

       if accessGranted == true { 
        DispatchQueue.main.async(execute: { 
         getALLEvents 
        }) 
       } else { 

       } 
      }) 
     } 

func getALLEvents(){ 
    let caledar = NSCalendar.current 
    let oneDayAgoComponents = NSDateComponents.init() 
    oneDayAgoComponents.day = -1 
    let oneDayAgo = caledar.date(byAdding: oneDayAgoComponents as DateComponents, to: Date(), wrappingComponents: true) 


    let oneYearFromNowComponents = NSDateComponents.init() 
    oneYearFromNowComponents.year = 1 
    let oneYearFromNow = caledar.date(byAdding: oneYearFromNowComponents as DateComponents, to: Date(), wrappingComponents: true) 

    let store = EKEventStore.init() 

    let predicate = store.predicateForEvents(withStart: oneDayAgo!, end: oneYearFromNow!, calendars: nil) 

    let event = store.events(matching: predicate) 
} 
関連する問題