使用Swift将事件添加到iOS8中的日历

时间:2016-06-29 06:23:20

标签: ios xcode swift events calendar

我是swift的新手,我在这个网站上练习“添加日历活动”:http://www.ioscreator.com/tutorials/add-event-calendar-tutorial-ios8-swift,但我在行中遇到以下错误

 let result = store.saveEvent(event, span: .ThisEvent, commit: error)
  • Call can throw, but it is not marked with 'try' and the error is not handled
  • Cannot convert value of type 'NSError?' to expected argument type 'Bool'

代码:

import UIKit
import EventKit

class ViewController: UIViewController {
    override func viewDidLoad() {
        super.viewDidLoad()
        let eventStore = EKEventStore()
        switch EKEventStore.authorizationStatusForEntityType(EKEntityType.Event) {
        case .Authorized:
            insertEvent(eventStore)
        case .Denied:
            print("Access denied")
        case .NotDetermined:
            // 3
            eventStore.requestAccessToEntityType(EKEntityType.Event, completion:
                {[weak self] (granted: Bool, error: NSError?) -> Void in
                    if granted {
                        self!.insertEvent(eventStore)
                    } else {
                        print("Access denied")
                    }
                })
        default:
            print("Case Default")
        }

    }

    override func didReceiveMemoryWarning() {
        super.didReceiveMemoryWarning()
        // Dispose of any resources that can be recreated.
    }

    func insertEvent(store : EKEventStore){
        let calendars = store.calendarsForEntityType(EKEntityType.Event) as! [EKCalendar]
        for calendar in calendars{
            if calendar.title == "ioscreater"{
                let startDate = NSDate()
                let endDate = startDate.dateByAddingTimeInterval(2*60*60)
                //Create event
                var event = EKEvent(eventStore : store)
                event.calendar = calendar
                event.title = "New Meeting"
                event.startDate = startDate
                event.endDate = endDate
                //Save event in Calendar
                var error : NSError?
                let result = store.saveEvent(event, span: .ThisEvent, commit: error)
                if result == false{
                    if let theError = error{
                        print ("An error occured \(theError)")
                    }
                }
            }

        }

    }

}

我搜索了错误,但找不到合适的解决方案。

3 个答案:

答案 0 :(得分:1)

func insertEvent(store : EKEventStore){
    let calendars = store.calendarsForEntityType(EKEntityType.Event)
    for calendar in calendars{
        if calendar.title == "ioscreater"{
            let startDate = NSDate()
            let endDate = startDate.dateByAddingTimeInterval(2*60*60)
            //Create event
            var event = EKEvent(eventStore : store)
            event.calendar = calendar
            event.title = "New Meeting"
            event.startDate = startDate
            event.endDate = endDate
            //Save event in Calendar
            do {
                try store.saveEvent(event, span: .ThisEvent)
            } catch {
                // Do error stuff here
            }
        }

答案 1 :(得分:1)

这适用于 Swift 3.0

首先,您需要添加"隐私 - 日历使用说明"在info.plist。

import EventKit

func addEventToCalendar(title: String, description: String?, startDate: Date, endDate: Date, completion: ((_ success: Bool, _ error: NSError?) -> Void)? = nil) {

     let eventStore = EKEventStore()

     eventStore.requestAccess(to: .event, completion: { (granted, error) in
        if (granted) && (error == nil) {
           let event = EKEvent(eventStore: eventStore)
           event.title = title
           event.startDate = startDate
           event.endDate = endDate
           event.notes = description
           event.calendar = eventStore.defaultCalendarForNewEvents
           do {
               try eventStore.save(event, span: .thisEvent)
           } catch let e as NSError {
               completion?(false, e)
               return
           }
           completion?(true, nil)
        } else {
           completion?(false, error as NSError?)
        }
    }) 
}

然后,您可以轻松地调用此方法,如下所示:

addEventToCalendar(title: "Best friend birthday", description: "Remember you or miss you!", startDate: NSDate(), endDate: NSDate())

谢谢并享受编码!!!

答案 2 :(得分:0)

在try中添加saveEvent代码,如下所示:

      do {
         try store.saveEvent(event, span: .ThisEvent)
                } catch let err as NSError{
                     print ("An error occured \(err.description)")
                }