4
votes

I have been working on an app to retrieve events from a Google Calendar using the Google Calendar API. They had a nice procedure and swift code on the web site back in May 2018 and I got it working in my app. With the procedure, I am able to retrieve the events from the Google Calendar into my app. Now, I want to create an event or change an event in my app and have it imported into the Google Calendar. Went to the Google Calendar API web site is completely changed and is replaced with G Suite APIs for iOS. I am unable to find anything related on the internet related to the old procedure. Can anyone working with Google Calendar API help? Any ideas on how to update Google Calendar API with a new event or a change in an event?

1
Hope you get an answer to this one. I'm finding that after I synchronize my Google Calendar with a Jobber calendar that the Calendar API's HTTP GET call pulls in events that I've created through my code, but doesn't pull in synchronized events.Will Buffington

1 Answers

1
votes

As for your original question, writing events to the Google Calendar, I was able to use the following function:

func writetoGC(token:String, startTime: String, endTime: String, summary: String, description: String) {

    let url = URL(string: "https://www.googleapis.com/calendar/v3/calendars/{YOUR CALENDAR ID HERE}/events")

    let summary1 = confirmationCode + "; " + summary

    let session = URLSession.shared
    print(session)
    var request = NSMutableURLRequest(url: url!)
    request.setValue("Bearer \(token)", forHTTPHeaderField: "Authorization")
    request.httpMethod = "POST"
    print(request)
    // token, startTime, endTime, summary, description

    var myHttpBody: Data = """
                            {
                            "end": {
                            "dateTime": "\(endTime)",
                            "timeZone": "America/Chicago"
                            },
                            "start": {
                            "dateTime": "\(startTime)",
                            "timeZone": "America/Chicago"
                            },
                            "summary": "\(summary1)",
                            "description": "\(description)"
                            }
                        """.data(using: .utf8)! as Data
    do {
        request.httpBody = myHttpBody
    } catch let error {
        print(error.localizedDescription)
    }

    request.addValue("application/json", forHTTPHeaderField: "Content-Type")

    print("Request: ")
    print(request.description)
    print(request.allHTTPHeaderFields)
    print("Body is:")
    print(request.httpBody)

    let task = session.dataTask(with: request as URLRequest, completionHandler: { data, response, error in

        guard error == nil else {
            return
        }

        guard let data = data else {
            return
        }
        sleep(1)
        do {
            //create json object from data
            if let json = try JSONSerialization.jsonObject(with: data, options: .mutableContainers) as? [String: Any] {
                print("Response is:")
                print(json)
                print("Description:")
                print(json.description)
                print("Debug Description:")
                print(json.debugDescription)

                // handle json...
            }
        } catch let error {
            print("Error during Serialization:")
            print(error.localizedDescription)
        }
    })
    task.resume()

    //verifyEntry()
}

So in this code I'm passing in a confirmation code in addition to the summary of the events. The confirmation code is one that I generate myself. You won't need that portion of the code unless you are needing a confirmation code in your summary. As you can see, I've already gained my OAuth 2.0 token and fed it into the function.

As for editing events, it shouldn't be hard to modify the code above to change an event.