是的,有一个适用于 iOS 的 People API 客户端库。要包含它,您需要指定
pod 'GoogleAPIClientForREST/PeopleService', '~> 1.3.4'
pod 'GoogleSignIn', '~> 4.1.2'
在 Cocoapods pod 文件中。
不,除了https://github.com/google/google-api-objectivec-client-for-rest 的源库本身之外,我没有找到任何适用于 iOS 的 Google People API 客户端库的文档
使用 Google Calendar API iOS Swift 示例代码作为指导,设置
private let scopes = [kGTLRAuthScopePeopleServiceContactsReadonly]
private let service = GTLRPeopleServiceService()
以下代码读取已登录用户的联系人列表。
// 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 要使 Google Calendar API iOS Swift 示例构建没有错误,您可能需要添加一个桥接头文件(文件 > 新建 > 文件,选择头文件)并添加以下内容:
#import <GTMSessionFetcher/GTMSessionFetcher.h>
#import <GTMSessionFetcher/GTMSessionFetcherService.h>
然后在 Swift Compiler - General 标题下的目标 Build Settings 中,在 ObjectiveC Bridging 标题行中添加
projectname/bridgingheaderfilename
您可能还需要清理构建文件夹(产品菜单,按住选项键)。