问题
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?
回答1:
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).
回答2:
Adding on to Hugo's answer in Swift 4.2 you need to authenticate with the GIDSignInDelegate
if you use the GoogleSignIn
cocoapod, once you conform to this protocol you can add the following to your class init() along with an optional String
private var accessToken: String?
//init
GIDSignIn.sharedInstance().clientID = "Your client id via https://console.developers.google.com/apis/api/people.googleapis.com"
GIDSignIn.sharedInstance().delegate = self
func sign(_ signIn: GIDSignIn!, didSignInFor user: GIDGoogleUser!, withError error: Error!) {
if let error = error {
print("Google sign in error: \(String(describing: error.localizedDescription))")
return
}
guard let authentication = user.authentication else { return }
self.accessToken = authentication.accessToken
self.fetchContacts()
}
Now fetchContacts()
looks something like this:
func fetchContacts() {
let query = GTLRPeopleServiceQuery_PeopleConnectionsList.query(withResourceName: "people/me")
let formattedToken = String(format: "Bearer %@", self.accessToken!)
let headers = ["Authorization": formattedToken, "3.0": "GData-Version"]
query.additionalHTTPHeaders = headers
query.personFields = "names,emailAddresses,photos"
query.pageSize = 2000 //max
services.shouldFetchNextPages = true
services.executeQuery(
query,
delegate: self,
didFinish: #selector(getCreatorFromTicket(ticket:finishedWithObject:error:)))
}
In AppDelegate.swift you need to call this method as well so that at the end of the authentication the url is properly handled
func application(_ app: UIApplication, open url: URL, options: [UIApplication.OpenURLOptionsKey : Any] = [:]) -> Bool {
return GIDSignIn.sharedInstance().handle(url,
sourceApplication:options[UIApplication.OpenURLOptionsKey.sourceApplication] as? String,
annotation: [:])
}
来源:https://stackoverflow.com/questions/50129233/is-there-any-sample-example-available-to-use-google-people-api-for-ios