首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >在Xcode11.5中从AppDelegate访问ViewController (firebase谷歌登录)

在Xcode11.5中从AppDelegate访问ViewController (firebase谷歌登录)
EN

Stack Overflow用户
提问于 2020-05-26 12:37:47
回答 3查看 541关注 0票数 1

我正在为一个玩具应用程序实现Firebase Google登录。在谷歌登录后,该应用程序将从SignInViewController迁移到HomePageViewController。根据firebase指令(https://firebase.google.com/docs/auth/ios/google-signin),1. GDSignInButton安装在SignInViewController中2. AppDelegate应该实现GDSignInDelegate的sign()。

在AppDelegate中的sign()结束之前,我应该如何告诉SignInViewController,您可以转到HomePageViewController。我已经成功地按照说明完成了登录。但是我不知道如何在AppDelegate中转换视图控制器。

请注意,在Xcode11.5中,有一个SceneDelegate,它拥有一个在旧版本中属于AppDelegate的窗口。因此,我们无法在AppDelegate中获得对包含视图控制器的窗口的引用。提前感谢!

我的问题解决了。感谢那些帮助我们的人!但我的问题仍然存在:假设你在AppDelegate中,你如何与(访问)你的观点进行交流?

EN

回答 3

Stack Overflow用户

发布于 2020-05-28 05:28:38

以下是我在iOS 13中执行此操作的代码:

AppDelegate.swift

代码语言:javascript
复制
import Firebase
import GoogleSignIn

@UIApplicationMain
class AppDelegate: UIResponder, UIApplicationDelegate {

    // configure de firebase app
    func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplication.LaunchOptionsKey: Any]?) -> Bool {
        FirebaseApp.configure()
        return true
    }

    // other methods in app delegate

    @available(iOS 9.0, *)
    func application(_ application: UIApplication, open url: URL, options: [UIApplication.OpenURLOptionsKey : Any]) -> Bool {
        return GIDSignIn.sharedInstance().handle(url)
    }
}

SceneDelegate.swift

代码语言:javascript
复制
import Firebase
import GoogleSignIn
import FirebaseAuth

// conform to the google sign in delegate
class SceneDelegate: UIResponder, UIWindowSceneDelegate, GIDSignInDelegate {

    var window: UIWindow?

    // when the app launches, it checks if the user is signed in, and redirect to the correct view controller
    func scene(_ scene: UIScene, willConnectTo session: UISceneSession, options connectionOptions: UIScene.ConnectionOptions) {
        let storyboard =  UIStoryboard(name: "Main", bundle: nil)
        if let windowScene = scene as? UIWindowScene {
            self.window = UIWindow(windowScene: windowScene)
            if Auth.auth().currentUser != nil {
                // redirect to the home controller
                self.window!.rootViewController = storyboard.instantiateViewController(withIdentifier: "HomeTabBarController")
                self.window!.makeKeyAndVisible()
            } else {
                // redirect to the login controller
                self.window!.rootViewController = storyboard.instantiateViewController(withIdentifier: "LoginViewController")
                self.window!.makeKeyAndVisible()
            }
        }
        GIDSignIn.sharedInstance().clientID = FirebaseApp.app()?.options.clientID
        GIDSignIn.sharedInstance().delegate = self
    }

    // handle the sign in to redirect to the home controller
    func sign(_ signIn: GIDSignIn!, didSignInFor user: GIDGoogleUser!, withError error: Error?) {
        if error != nil {
            return
        }

        guard let authentication = user.authentication else { return }
        let credential = GoogleAuthProvider.credential(withIDToken: authentication.idToken, accessToken: authentication.accessToken)
        Auth.auth().signIn(with: credential) { (authResult, error) in
            if let error = error {
                print(error.localizedDescription)
                return
            }
            let storyboard =  UIStoryboard(name: "Main", bundle: nil)
            // redirect the user to the home controller
            self.window!.rootViewController = storyboard.instantiateViewController(withIdentifier: "HomeTabBarController")
            self.window!.makeKeyAndVisible()
        }
    }

    func sign(_ signIn: GIDSignIn!, didDisconnectWith user: GIDGoogleUser!, withError error: Error!) {
        // Perform any operations when the user disconnects from app here.
        // ...

    }


}
票数 2
EN

Stack Overflow用户

发布于 2020-05-26 13:30:37

如果您的项目使用SceneDelegate,则必须使用以下代码来访问key窗口。在keyWindow中,您可以使用.rootViewController访问rootViewController,并将其向下转换为MyViewController并相应地访问它。

代码语言:javascript
复制
let keyWindow = UIApplication.shared.connectedScenes
        .filter({$0.activationState == .foregroundActive})
        .map({$0 as? UIWindowScene})
        .compactMap({$0})
        .first?.windows
        .filter({$0.isKeyWindow}).first
let myViewContorller = keyWindow?.rootViewController as? MyViewController
票数 0
EN

Stack Overflow用户

发布于 2020-05-27 12:55:14

在appDelegate中:

可以通过将其作为RootViewController传递

代码语言:javascript
复制
func application(application: UIApplication, didFinishLaunchingWithOptions
launchOptions: [NSObject: AnyObject]?) -> Bool {
    self.window = UIWindow(frame: UIScreen.main.bounds)
     let storyBoard : UIStoryboard = UIStoryboard(name: "Main", bundle: nil)
     let viewController : UIViewController = storyBoard.instantiateViewControllerWithIdentifier(“InitialControllerID”) as InitialViewController
     self.window?.rootViewController = viewController
     self.window?.makeKeyAndVisible()

     return true
}

或者,如果rootViewController是UINavigation控制器类型,则将您的主屏幕推到它上面,如下所示:

代码语言:javascript
复制
func application(application: UIApplication, didFinishLaunchingWithOptions launchOptions: [NSObject: AnyObject]?) -> Bool {
let rootViewController = self.window!.rootViewController as! UINavigationController
let mainStoryboard = UIStoryboard(name: "Main", bundle: nil)
let viewController = mainStoryboard.instantiateViewController(withIdentifier: “InitialControllerID") as! InitialViewController
rootViewController.pushViewController(viewController, animated: true)
return true
}

基于这篇文章:-“https://www.dev2qa.com/how-to-set-application-root-view-controller-programmatically-in-xcode-11/

代码语言:javascript
复制
For iOS 13
In SceneDelegate.swift 

func scene(_ scene: UIScene, willConnectTo session: UISceneSession, options `connectionOptions: UIScene.ConnectionOptions) {`

// If this scene's self.window is nil then set a new UIWindow object to it.
self.window = self.window ?? UIWindow()

// Set this scene's window's background color.
self.window!.backgroundColor = UIColor.red

// Create a ViewController object and set it as the scene's window's root view controller.
self.window!.rootViewController = ViewController()

// Make this scene's window be visible.
self.window!.makeKeyAndVisible()

guard scene is UIWindowScene else { return }
}
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/62014779

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档