是否有任何示例/示例可供使用Google People API for iOS?

时间:2018-05-02 07:29:20

标签: ios swift google-people

我查看了Google People API的文档。

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

我无法找到任何帮助/示例来为iOS平台实现相同的功能。 iOS平台有什么帮助吗?

2 个答案:

答案 0 :(得分:4)

是的,有适用于iOS的People API客户端库。要包含它,请指定

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

在Cocoapods pod文件中。

不,我没有找到任何适用于iOS的Google People API客户端库的文档,除了https://github.com/google/google-api-objectivec-client-for-rest

上的源库本身

使用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编译器下的目标Build Settings - General标题中,在ObjectiveC Bridging标题行中添加

projectname/bridgingheaderfilename

您可能还必须清理构建文件夹(“产品”菜单,按住选项键)。

答案 1 :(得分:0)

在Swift 4.2中添加到Hugo的答案上,如果您使用GIDSignInDelegate cocoapod,则需要使用GoogleSignIn进行身份验证,一旦遵循此协议,就可以将以下内容添加到类init()中以及可选的字符串

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()
}

现在fetchContacts()看起来像这样:

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:)))
}

在AppDelegate.swift中,您还需要调用此方法,以便在身份验证结束时正确处理网址

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: [:])
}