ios - GPPSignInDelegate 的实现是否必须是 ViewController 或 AppDelegate?

标签 ios google-plus

我正在尝试按照说明将我用 Swift 编写的 iOS 应用程序与 Google Plus 集成 here .在 ViewController 中完成所有工作后,我愚蠢地认为我可以将身份验证行为封装在它自己的类中(如下)

问题是两个 GPPSignInDelegate 协议(protocol)函数 finishedwithAuthdidDisconnectWithError 永远不会被调用。如果我传入一个 ViewController 或实现 GPPSignInDelegateAppDelegate 并将其分配给 googlePlus.delegate 回调调用成功;当我将 self 分配给委托(delegate)时,永远不会调用回调。

import Foundation


class GoogleAuthentication : GPPSignInDelegate  {

  let kClientId: NSString = ""
  let googlePlus: GPPSignIn
  var authenticatedSuccessfully: Bool?

  // Passing in a ViewController implementing GPPSignInDelegate works
  // init(delegate: GPPSignInDelegate) {    
  init() {
      self.delegate = delegate
      googlePlus = GPPSignIn.sharedInstance()
      googlePlus.shouldFetchGooglePlusUser = true
      googlePlus.clientID = kClientId
      googlePlus.scopes = [kGTLAuthScopePlusLogin]
      googlePlus.delegate = self // works when assignging delegate passed in

      authenticatedSuccessfully = googlePlus.trySilentAuthentication()
  }

  func authenticate() {
      googlePlus.authenticate()
  }

  func finishedWithAuth(auth: GTMOAuth2Authentication!, error: NSError!) {
      println("finished with auth!")
  }

  func didDisconnectWithError(error: NSError!) {
      println("disconnected with error!")
  }
}

我是否遗漏了什么,或者 GPPSignInDelegate 是否必须由 ViewControllerAppDelegate 实现?如果是这样,为什么?

最佳答案

委托(delegate)对象必须继承自 NSObject。于是就变成了:

class GoogleAuthentication: NSObject, GPPSignInDelegate  {

关于ios - GPPSignInDelegate 的实现是否必须是 ViewController 或 AppDelegate?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/26630477/

相关文章:

ios - 如何在 MGL mapView 中缩放,以便 View 包含 map 上的一个 "object"

ios - Firebase 数据库架构 - 像 Snapchat

ios - Google Plus 按钮在 Swift iOS 中不起作用

javascript - Google 官方测试页上的 Google Plus 登录按钮错误事件

ios - 从 objective-c 文件访问swift文件中的共享数据

android - 获取正在运行的应用程序 flutter

iOS内存管理属性

javascript - 如何创建类似滚动 UI 的 Google Plus?

google-plus - 如何将自定义标题、描述和图像传递给 google plus 按钮,如 facebook 共享按钮

google-plus - Google+ 退出时不弹出对话框