【问题标题】:How do I access the variables that google provides outside of the function?如何访问谷歌在函数之外提供的变量?
【发布时间】:2022-11-15 22:00:27
【问题描述】:

我正在尝试在不同的视图控制器中访问 emailAddress 变量,但它始终不在范围内。

我想打电话给

login.emailAddress

在不同的vc中。

这是我的代码供您参考,我知道也有类似的问题,但是我很难将其翻译成我的代码。 .

import UIKit
import GoogleSignIn

let login = LoginController()

class LoginController: UIViewController {
    
    @IBOutlet weak var signInButton: GIDSignInButton!
    
    let signInConfig = GIDConfiguration(clientID: "12345-abcdef.apps.googleusercontent.com")
    
        @IBAction func signIn(_ sender: Any)  {
            GIDSignIn.sharedInstance.signIn(with: signInConfig, presenting: self) { user, error in
                guard error == nil else { return }
                guard let user = user else { return }

                var emailAddress = user.profile?.email

                var fullName = user.profile?.name
                var givenName = user.profile?.givenName
                var familyName = user.profile?.familyName

                var profilePicUrl = user.profile?.imageURL(withDimension: 320)
                
                let userProfile = (fullName, givenName, emailAddress, profilePicUrl)

                print("Sign in Sucessfull")
                print(fullName!)
                print(givenName!)
                print(familyName!)
                print(emailAddress!)
                print(profilePicUrl!)
                
                // If sign in succeeded, display the app's main content View.
                
                let vc = self.storyboard?.instantiateViewController(withIdentifier: "NavigationViewController") as! UINavigationController
                self.navigationController?.pushViewController(vc, animated: true)
                self.present(vc, animated: true, completion: nil)
            }
        }
}

【问题讨论】:

    标签: swift xcode storyboard


    【解决方案1】:

    一个选项是将值存储在类属性中:

    class LoginController: UIViewController {
    
    @IBOutlet weak var signInButton: GIDSignInButton!
    private var userMail: String? 
    
    let signInConfig = GIDConfiguration(clientID: "12345-abcdef.apps.googleusercontent.com")
    
        @IBAction func signIn(_ sender: Any)  {
            GIDSignIn.sharedInstance.signIn(with: signInConfig, presenting: self) { user, error in
                guard error == nil else { return }
                guard let user = user else { return }
    
                self.userMail = user.profile?.email
                [...]
            }
        }
    

    }

    【讨论】:

    • 您既是君子又是书生!我很欣赏您的回答的简单性,并且您为我提供了前进所需的示例!
    猜你喜欢
    • 2014-07-11
    • 1970-01-01
    • 1970-01-01
    • 2018-10-16
    • 2017-09-15
    • 2014-04-09
    • 2014-02-18
    • 2017-09-11
    • 1970-01-01
    相关资源
    最近更新 更多