草庐IT

ios - Google 登录不在 UIVIewController 中

coder 2023-09-16 原文

我正在尝试创建一个类来处理我项目中的所有登录服务。除了 Google 登录外,一切正常。

我想在 UIVIewController 中实现 Google 登录,如 Google 文档中所述,您可以使用 GIDSignInDelegate 实现此目的,并在您的类不是 UIViewController 时使用它。

我的类是从 NSObject 扩展而来的,并希望在该服务上处理 Google SignIn,之后我只想调用一个方法来执行此操作。

var ls: LoginService = LoginService.instance
 ls.doGoogleLogin()

这是我的登录服务:

struct LOGIN_URL {
    static let LOGIN: String = "login"
}

protocol LoginServiceDelegate: class {
    func loginSuccess(loginResponse: User)
    func loginFailed(message: String)
}

class LoginService: NSObject, GIDSignInDelegate{

    static let instance = LoginService()

    weak var delegate: LoginServiceDelegate?

    var user: User?

    func doGoogleLogin(){
        var configureError: NSError?
        GGLContext.sharedInstance().configureWithError(&configureError)
        assert(configureError == nil, "Error configuring Google services: \(configureError)")

        GIDSignIn.sharedInstance().delegate = self
//        GIDSignIn.sharedInstance().uiDelegate = self

        GIDSignIn.sharedInstance().signIn()
    }

    public func sign(_ signIn: GIDSignIn!, didSignInFor user: GIDGoogleUser!, withError error: Error!) {
        print(user.profile.email)
    }

    func signIn(signIn: GIDSignIn!,
                presentViewController viewController: UIViewController!) {
        self.presentViewController(viewController, animated: true, completion: nil)
    }

    func signIn(signIn: GIDSignIn!,
                dismissViewController viewController: UIViewController!) {
        self.dismissViewControllerAnimated(true, completion: nil)
    }
}

当我使用这段代码时出现错误 LoginService 类型的值没有成员 presentViewController

这里应该怎么做才不会报错?

最佳答案

通过键入 self.presentViewController,您是说 self 引用的类是一个可以使用方法 presentViewController 的对象类型.在这种情况下,当您说 self 时,您引用的是 LoginService 实例。 LoginService 继承自 NSObject,这意味着它不是一个对象 - 如 View ControllerNavigation Controller 可以调用该方法。

我认为您需要在 LoginService 类的顶部添加委托(delegate)方法。

protocol LoginServiceDelegate: class {
  func loginSuccess(loginResponse: User)
  func loginFailed(message: String)
  func googleSignInLaunch(_ viewController: UIViewController)
  func googleSignInDismiss(_ viewController: UIViewController)
}

然后当你打电话时:

func signIn(signIn: GIDSignIn!, presentViewController viewController: UIViewController!) {
    //call delegate method here
    //which will trigger view controller calling this to launch something
    delegate?.googleSignInLaunch(_ viewController: UIViewController)
}

func signIn(signIn: GIDSignIn!,
            dismissViewController viewController: UIViewController!) {
    delegate?.googleSignInDismiss(_ viewController: UIViewController)
}

然后在 View Controller 中调用您的登录服务:

class MyViewController: UIViewController {

  let loginService = LoginService()

  override func viewDidLoad() {
    loginService.delegate = self
    loginService.doGoogleLogin()
  }

  func googleSignInLaunch(_ viewController: UIViewController) {
    self.presentViewController(viewController, animated: true, completion: nil)
  }

  func googleSignInDismiss(_ viewController: UIViewController) {
    self.dismissViewControllerAnimated(true, completion: nil)
  }

  //the methods below are the other two methods that were in your LoginService protocol

  func loginSuccess(loginResponse: User) {
    //whatever you need to do
  }

  func loginFailed(message: String) {
    //whatever you need to do
  }

}

关于ios - Google 登录不在 UIVIewController 中,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/41021304/

有关ios - Google 登录不在 UIVIewController 中的更多相关文章

  1. ruby-on-rails - form_for 中不在模型中的自定义字段 - 2

    我想向我的Controller传递一个参数,它是一个简单的复选框,但我不知道如何在模型的form_for中引入它,这是我的观点:{:id=>'go_finance'}do|f|%>Transferirde:para:Entrada:"input",:placeholder=>"Quantofoiganho?"%>Saída:"output",:placeholder=>"Quantofoigasto?"%>Nota:我想做一个额外的复选框,但我该怎么做,模型中没有一个对象,而是一个要检查的对象,以便在Controller中创建一个ifelse,如果没有检查,请帮助我,非常感谢,谢谢

  2. ruby - 当使用::指定模块时,为什么 Ruby 不在更高范围内查找类? - 2

    我刚刚被困在这个问题上一段时间了。以这个基地为例:moduleTopclassTestendmoduleFooendend稍后,我可以通过这样做在Foo中定义扩展Test的类:moduleTopmoduleFooclassSomeTest但是,如果我尝试通过使用::指定模块来最小化缩进:moduleTop::FooclassFailure这失败了:NameError:uninitializedconstantTop::Foo::Test这是一个错误,还是仅仅是Ruby解析变量名的方式的逻辑结果? 最佳答案 Isthisabug,or

  3. ruby - 如何验证 IO.copy_stream 是否成功 - 2

    这里有一个很好的答案解释了如何在Ruby中下载文件而不将其加载到内存中:https://stackoverflow.com/a/29743394/4852737require'open-uri'download=open('http://example.com/image.png')IO.copy_stream(download,'~/image.png')我如何验证下载文件的IO.copy_stream调用是否真的成功——这意味着下载的文件与我打算下载的文件完全相同,而不是下载一半的损坏文件?documentation说IO.copy_stream返回它复制的字节数,但是当我还没有下

  4. Ruby 文件 IO 定界符? - 2

    我正在尝试解析一个文本文件,该文件每行包含可变数量的单词和数字,如下所示:foo4.500bar3.001.33foobar如何读取由空格而不是换行符分隔的文件?有什么方法可以设置File("file.txt").foreach方法以使用空格而不是换行符作为分隔符? 最佳答案 接受的答案将slurp文件,这可能是大文本文件的问题。更好的解决方案是IO.foreach.它是惯用的,将按字符流式传输文件:File.foreach(filename,""){|string|putsstring}包含“thisisanexample”结果的

  5. Get https://registry-1.docker.io/v2/: net/http: request canceled while waiting - 2

    1.错误信息:Errorresponsefromdaemon:Gethttps://registry-1.docker.io/v2/:net/http:requestcanceledwhilewaitingforconnection(Client.Timeoutexceededwhileawaitingheaders)或者:Errorresponsefromdaemon:Gethttps://registry-1.docker.io/v2/:net/http:TLShandshaketimeout2.报错原因:docker使用的镜像网址默认为国外,下载容易超时,需要修改成国内镜像地址(首先阿里

  6. ruby - 为什么不能使用类IO的实例方法noecho? - 2

    print"Enteryourpassword:"pass=STDIN.noecho(&:gets)puts"Yourpasswordis#{pass}!"输出:Enteryourpassword:input.rb:2:in`':undefinedmethod`noecho'for#>(NoMethodError) 最佳答案 一开始require'io/console'后来的Ruby1.9.3 关于ruby-为什么不能使用类IO的实例方法noecho?,我们在StackOverflow上

  7. ruby - Google-api-ruby-client 翻译 API 示例 - 2

    很高兴看到google代码:google-api-ruby-client项目,因为这对我来说意味着Ruby人员可以使用GoogleAPI-s来完善代码。虽然我现在很困惑,因为给出的唯一示例使用Buzz,并且根据我的实验,Google翻译(v2)api的行为必须与google-api-ruby-client中的Buzz完全不同。.我对“Explorer”演示示例很感兴趣——但据我所知,它并不是一个探索器。它所做的只是调用一个Buzz服务,然后浏览它已经知道的关于Buzz服务的事情。对我来说,Explorer应该让您“发现”所公开的服务和方法/功能,而不一定已经知道它们。我很想听听使用这个

  8. ruby - 使用 Ruby 和 Mechanize 登录网站 - 2

    我需要从站点抓取数据,但它需要我先登录。我一直在使用hpricot成功地抓取其他网站,但我是使用mechanize的新手,我真的对如何使用它感到困惑。我看到这个例子经常被引用:require'rubygems'require'mechanize'a=Mechanize.newa.get('http://rubyforge.org/')do|page|#Clicktheloginlinklogin_page=a.click(page.link_with(:text=>/LogIn/))#Submittheloginformmy_page=login_page.form_with(:act

  9. ruby - 如何从 URL 中删除 Google 跟踪参数 (UTM)? - 2

    我有一堆要清理的URL。它们都包含UTM参数,在这种情况下不是必需的,或者是有害的。示例:http://houseofbuttons.tumblr.com/post/22326009438?utm_source=feedburner&utm_medium=feed&utm_campaign=Feed%3A+HouseOfButtons+%28House+of+Buttons%29所有可能的参数都以utm_开头。如何使用ruby​​脚本/结构轻松删除它们而不破坏其他潜在的“好”URL参数? 最佳答案 您可以将正则表达式应用于url以清

  10. ruby-on-rails - 使用用户或管理员模型和 Basecamp 样式子域设计登录 - 2

    我为Devise用户和管理员提供了不同的模型。我也在使用Basecamp风格的子域。除了我需要能够以用户或管理员身份进行身份验证的一些Controller和操作外,一切都运行良好。目前我有authenticate_user!在我的application_controller.rb中设置,对于那些只有管理员才能访问的Controller和操作,我使用skip_before_filter跳过它。不幸的是,我不能简单地指定每个Controller的身份验证要求,因为我仍然需要一些Controller和操作才能被用户或管理员访问。我尝试了一些方法都无济于事。看来,如果我移动authentica

随机推荐