试图在 Swift 中的框架和应用程序之间实现 Google Sign?

问题描述

我已经了解了所有与堆栈溢出答案相关的 Google 签名解决方案,但没有一个有帮助。

我正在创建一个具有 google 登录功能的框架。我可以成功登录,但问题是在完成登录后没有调用委托方法。我尝试了很多解决方案,但委托方法不起作用。这里通过以下方式我已经实现了:

框架实现

public class Framework {

   public init() {}

   public class func initialization(clientId: String) {
       GIDSignIn.sharedInstance()?.clientID = clientId
   }

   public func loadView(in container: UIView) {
       let bundle = Bundle(identifier: "com.framework")
       let storyboard = UIStoryboard(name: "Frameworks",bundle: bundle)
       let vc = storyboard.instantiateViewController(identifier: "ViewControllerID") as ViewController
       vc.view.frame = CGRect(x: 0,y: 0,width: container.bounds.width,height: container.bounds.height)
       container.addSubview(vc.view)
   }
}
class ViewController: UIViewController {
    
    override func viewDidLoad() {
        super.viewDidLoad()
        GIDSignIn.sharedInstance().presentingViewController = UIApplication.shared.windows.first?.rootViewController!
        GIDSignIn.sharedInstance().delegate = self
    }
    
    @IBAction func onGoogleSignInButtonTapped(_ sender: Any) {
        print("onGoogleSignInButtonTapped")
        GIDSignIn.sharedInstance().signIn()
    }
 
}

extension ViewController: GIDSignInDelegate {
    func sign(_ signIn: GIDSignIn!,didSignInFor user: GIDGoogleUser!,withError error: Error!) {
        if (error == nil) {

            // perform your log in functions here...
            print(user.profile.email!)

        } else {
            print(error.localizedDescription)
        }
    }
    
    func sign(_ signIn: GIDSignIn!,diddisconnectWith user: GIDGoogleUser!,withError error: Error!) {
        if let error = error {
            print(error.localizedDescription)
        }
    }
}

应用使用示例

    class AppDelegate: UIResponder,UIApplicationDelegate {
    
       func application(_ application: UIApplication,didFinishLaunchingWithOptions launchOptions: [UIApplication.LaunchOptionsKey: Any]?) -> Bool {
           Framework.initialization(clientId: "APP_CLIENT_ID")
           return true
       }
    }
class ViewController: UIViewController {

   override func viewDidLoad() {
       super.viewDidLoad()
            
       let framework = Framework()
       framework.loadView(on: containerView)
   }
}

解决方法

暂无找到可以解决该程序问题的有效方法,小编努力寻找整理中!

如果你已经找到好的解决方法,欢迎将解决方案带上本链接一起发送给小编。

小编邮箱:dio#foxmail.com (将#修改为@)