Join the social network of Tech Nerds, increase skill rank, get work, manage projects...
 
  • How to Add Events to iOS Calendar in Your iPhone Device

    • 0
    • 0
    • 0
    • 0
    • 0
    • 0
    • 1
    • 0
    • 832
    Comment on it

    To add an event to iOS calendar we need to access calendar of the iPhone without accessing calendar we can not add events in iPhone.

     


    For accessing iPhone calendar we have to ask for permissions first. Here permission means allowing the particular app or functionality officially to do a particular thing on your device/system like setting up event and alert on your device or accessing your data, location etc.

     

    So for requesting permissions in iPhone you can use below code example:

    1. func isCalendarAccesible(completion: @escaping (( _ isAllowed:Bool , _ error:Error?)->Void)) {
    2. let status = EKEventStore.authorizationStatus(for: .event)
    3. switch (status){
    4. case .notDetermined:
    5. // This happens on first-run
    6. requestAccessToCalendar(completion: completion)
    7. case .authorized:
    8. // User has access
    9. print("User has access to calendar")
    10. completion(true, nil)
    11. case EKAuthorizationStatus.restricted, EKAuthorizationStatus.denied:
    12. // We need to help them give us permission
    13. noPermission()
    14.  
    15. completion(false, nil)
    16. }
    17. }
    18. func noPermission() {
    19. print("User has to change settings...goto settings to view access")
    20. }
    21. func requestAccessToCalendar(completion: @escaping (( _ isAllowed:Bool , _ error:Error?)->Void)) {
    22. EventHelper.appleEventStore.requestAccess(to: .event, completion: { (granted, error) in
    23. if (granted) && (error == nil) {
    24. DispatchQueue.main.async {
    25. print("User has access to calendar")
    26. completion(true, nil)
    27. }
    28. } else {
    29. DispatchQueue.main.async{
    30. self.noPermission()
    31. completion(false, error)
    32. }
    33. }
    34. })
    35. }
    36. func addAppleEvents(titile:String,startDate:Date,endDate:Date,notes:String) {
    37. let event:EKEvent = EKEvent(eventStore: EventHelper.appleEventStore)
    38. event.title = titile
    39. event.startDate = startDate
    40. event.endDate = endDate
    41. event.notes = notes
    42. event.calendar = EventHelper.appleEventStore.defaultCalendarForNewEvents
    43. event.calendar.title = "AppTitle"
    44. do {
    45. let interval:TimeInterval = 0;
    46. let alarm = EKAlarm(relativeOffset: interval)
    47. event.addAlarm(alarm)
    48. try EventHelper.appleEventStore.save(event, span: .thisEvent)
    49. print("events added with dates:")
    50. } catch let e as NSError {
    51. print("Error in saving events" + e.description)
    52. return
    53. }
    54. print("Saved Event")
    55. }

     

    Once above code is excuted you have to call the below method, to call this method by using the below code:

    1. isCalendarAccesible { (allowed, error) in
    2.  
    3. //first we will check if permission is granted to app for accessing our iPhone calendar. If it is true then we can create our event from following code
    4. if allowed == true {
    5. addAppleEvents(titile: "birthday Testing event for my app", startDate: YOUR_EVENT_START_DATE , endDate:YOUR_EVENT_END_DATE,notes: "birthday testing event")
    6. }
    7. }

     

    Output:  After calling the method by above process, you will get output as in below screenshot, to see the output you have to open your iPhone calendar

    How to Add Events to iOS Calendar in Your iPhone Device

 0 Comment(s)

Sign In
                           OR                           
                           OR                           
Register

Sign up using

                           OR                           
Forgot Password
Reset Password
Fill out the form below and reset your password: