Getting NSError domain: “com.google.GTLJSONRPCErrorDomain” while fetching Google Calendar events in Swift

前端 未结 1 499
北恋
北恋 2021-01-16 16:54

I am trying to fetch Calendar events to my iOS application. So, for that I have create a project in Google developers console and got Client ID, ApiKey.

And installe

相关标签:
1条回答
  • 2021-01-16 17:42

    Swift - 4

    I doesn't recognise why you are getting that error. I think you are not using latest pods. Refer following code, that I have used for fetch google calendar events.

    Pods: - pod 'GoogleAPIClientForREST/Calendar', '~> 1.2.1'

    pod 'Google/SignIn', '~> 3.0.3'

    import UIKit
    import GoogleAPIClientForREST
    import GoogleSignIn
    class ClassForCalEvent: UIViewController, GIDSignInDelegate, GIDSignInUIDelegate {
    
        let eventArray = NSMutableArray()
        private let scopes = [kGTLRAuthScopeCalendar]
    
        private let service = GTLRCalendarService()
        override func viewDidLoad() {
            super.viewDidLoad()
    
              // Configure Google Sign-in.
                GIDSignIn.sharedInstance().delegate = self
                GIDSignIn.sharedInstance().uiDelegate = self
                GIDSignIn.sharedInstance().scopes = scopes
                GIDSignIn.sharedInstance().signIn()
    
        }
    
    func sign(_ signIn: GIDSignIn!, didSignInFor user: GIDGoogleUser!,
                  withError error: Error!) {
            if let error = error {
                showAlert(title: "Authentication Error", message: error.localizedDescription)
                self.service.authorizer = nil
            } else {
                            self.service.authorizer = user.authentication.fetcherAuthorizer()
                fetchEvents()
            }
        }
    
        // Construct a query and get a list of upcoming events from the user calendar
        func fetchEvents() {
            let query = GTLRCalendarQuery_EventsList.query(withCalendarId: "primary")
            query.maxResults = 10
            query.timeMin = GTLRDateTime(date: Date())
            query.singleEvents = true
            query.orderBy = kGTLRCalendarOrderByStartTime
            service.executeQuery(
                query,
                delegate: self,
                didFinish: #selector(displayResultWithTicket(ticket:finishedWithObject:error:)))
        }
    
        // Display the start dates and event summaries in the UITextView
        @objc func displayResultWithTicket(
            ticket: GTLRServiceTicket,
            finishedWithObject response : GTLRCalendar_Events,
            error : NSError?) {
    
            if let error = error {
                showAlert(title: "Error", message: error.localizedDescription)
                return
            }
    
            var outputText = ""
            if let events = response.items, !events.isEmpty {
                for event in events {
                    let start = event.start!.dateTime ?? event.start!.date!
                    let startString = DateFormatter.localizedString(
                        from: start.date,
                        dateStyle: .short,
                        timeStyle: .short)
    
                    let dataDic = ["start_Time":startString , "tag":"red", "Event_Name":event.summary!] 
                    eventArray.add(dataDic)
    
                    outputText += "\(startString) - \(event.summary!)\n"
                    print("Output ", startString , event )
                }
                UserDefaults.standard.set(NSKeyedArchiver.archivedData(withRootObject: eventArray), forKey: "EventArray")
    
                UserDefaults.standard.synchronize()
            } else
            {
                outputText = "No upcoming events found."
            }
            print("Output Text",outputText)
            showAlert(title: "Event Sync Succesfully!", message:"")
        }
    
    0 讨论(0)
提交回复
热议问题