Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Is there any sample/example available to use Google People API for iOS?

I went through he documentation for Google People API.

https://developers.google.com/people/v1/getting-started

I couldn't find any help/sample to implement the same for iOS platform. Is there any help available for iOS platform?

like image 582
Niraj Avatar asked Dec 08 '25 06:12

Niraj


1 Answers

Yes there is a People API client library for iOS. To include it you specify

pod 'GoogleAPIClientForREST/PeopleService', '~> 1.3.4'
pod 'GoogleSignIn', '~> 4.1.2'

in the Cocoapods pod file.

No, I havn't found any documentation of the Google People API client library for iOS except the source libraries themselves at https://github.com/google/google-api-objectivec-client-for-rest

Use the Google Calendar API iOS Swift sample code as guidance, setting

private let scopes = [kGTLRAuthScopePeopleServiceContactsReadonly]
private let service = GTLRPeopleServiceService()

The following code reads the contacts list for the signed in user.

// MARK: - Get Google Contacts

@objc func fetchContacts() {
    let query = GTLRPeopleServiceQuery_PeopleConnectionsList.query(withResourceName: "people/me")
    query.personFields = "names,emailAddresses,photos"
    service2.executeQuery(
        query,
        delegate: self,
        didFinish: #selector(getCreatorFromTicket(ticket:finishedWithObject:error:)))
}

@objc func getCreatorFromTicket(
    ticket: GTLRServiceTicket,
    finishedWithObject response: GTLRPeopleService_ListConnectionsResponse,
    error: NSError?) {

    if let error = error {
        showAlert(title: "Error", message: error.localizedDescription)
        return
    }

    if let connections = response.connections, !connections.isEmpty {
        for connection in connections {
            if let names = connection.names, !names.isEmpty {
                for name in names {
                    if let _ = name.metadata?.primary {
                        print(name.displayName ?? "")
                    }
                }
            }
            if let emailAddresses = connection.emailAddresses, !emailAddresses.isEmpty {
                for email in emailAddresses {
                        if let _ = email.metadata?.primary {
                    print(email.value ?? "")
                    }
                }
            }
            if let photos = connection.photos, !photos.isEmpty {
                for photo in photos {
                    if let _ = photo.metadata?.primary {
                        print(photo.url ?? "")
                    }
                }
            }
        }
    }
}

ps To make the Google Calendar API iOS Swift sample build without errors, you probably need to add a bridging header file (File > New > File, choose header file) and add the following:

#import <GTMSessionFetcher/GTMSessionFetcher.h>
#import <GTMSessionFetcher/GTMSessionFetcherService.h>

Then in the target Build Settings under the Swift Compiler - General heading, in the ObjectiveC Bridging header row add

projectname/bridgingheaderfilename

You may also then have to clean the build folder (Product menu, hold down options key).

like image 188
Hugo F Avatar answered Dec 09 '25 18:12

Hugo F



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!