【问题标题】:Swift App not starting at Initial ViewController SceneSwift App 未在初始 ViewController 场景中启动
【发布时间】:2017-04-11 14:32:59
【问题描述】:

我目前正在制作一个应用程序,该应用程序具有 Google 登录功能,并已实施到项目中,但我不确定为什么该应用程序没有在 InitialViewController 场景中启动。它不是从初始场景开始,而是从我的登录页面开始。我认为这可能会发生,因为我将它设置为 ViewController.swift 文件与登录页面相关联,而不是与初始启动页面相关联。

这是我的 AppDelegate 的代码:

import UIKit
import Firebase
import GoogleSignIn

@UIApplicationMain
class AppDelegate: UIResponder, UIApplicationDelegate, GIDSignInDelegate {

    var window: UIWindow?


    func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplicationLaunchOptionsKey: Any]?) -> Bool {

        FIRApp.configure()

        GIDSignIn.sharedInstance().clientID = FIRApp.defaultApp()?.options.clientID
        GIDSignIn.sharedInstance().delegate = self

        if GIDSignIn.sharedInstance().hasAuthInKeychain() {
            print("User has been successfully signed in with Google")
            let sb = UIStoryboard(name: "Main", bundle: nil)
            if let tabBarVC = sb.instantiateViewController(withIdentifier: "TabController") as? UITabBarController {
                window!.rootViewController = tabBarVC
            }
        } else {
            print("User has failed in signing in with Google")
            let sb = UIStoryboard(name: "Main", bundle: nil)
            if let tabBarVC = sb.instantiateViewController(withIdentifier: "LogInViewController") as? ViewController {
                window!.rootViewController = tabBarVC
            }
        }

        return true
    }



    func sign(_ signIn: GIDSignIn!, didSignInFor user: GIDGoogleUser!, withError error: Error!) {
        if let err = error {
            print("Failed to log into Google: ", err)
            return
        }

        print("Successfully logged into Google", user)

        guard let idToken = user.authentication.idToken else { return }
        guard let accessToken = user.authentication.accessToken else { return }
        let credentials = FIRGoogleAuthProvider.credential(withIDToken: idToken, accessToken: accessToken)

        FIRAuth.auth()?.signIn(with: credentials, completion: { (user, error) in
            if let err = error {
                print("Failed to create a Firebase User with Google account: ", err)
                return
            }

            guard let uid = user?.uid else { return }
            print("Successfully logged into Firebase with Google", uid)
        })
    }


    func application(_ app: UIApplication, open url: URL, options: [UIApplicationOpenURLOptionsKey : Any] = [:]) -> Bool {



        GIDSignIn.sharedInstance().handle(url,
                                          sourceApplication: options[UIApplicationOpenURLOptionsKey.sourceApplication] as! String!,
                                          annotation: options[UIApplicationOpenURLOptionsKey.annotation])

        return true
    }

    func applicationWillResignActive(_ application: UIApplication) {
        // Sent when the application is about to move from active to inactive state. This can occur for certain types of temporary interruptions (such as an incoming phone call or SMS message) or when the user quits the application and it begins the transition to the background state.
        // Use this method to pause ongoing tasks, disable timers, and invalidate graphics rendering callbacks. Games should use this method to pause the game.
    }

    func applicationDidEnterBackground(_ application: UIApplication) {
        // Use this method to release shared resources, save user data, invalidate timers, and store enough application state information to restore your application to its current state in case it is terminated later.
        // If your application supports background execution, this method is called instead of applicationWillTerminate: when the user quits.
    }

    func applicationWillEnterForeground(_ application: UIApplication) {
        // Called as part of the transition from the background to the active state; here you can undo many of the changes made on entering the background.
    }

    func applicationDidBecomeActive(_ application: UIApplication) {
        // Restart any tasks that were paused (or not yet started) while the application was inactive. If the application was previously in the background, optionally refresh the user interface.
    }

    func applicationWillTerminate(_ application: UIApplication) {
        // Called when the application is about to terminate. Save data if appropriate. See also applicationDidEnterBackground:.
    }


}

和 ViewController

import UIKit
import Firebase
import GoogleSignIn

class ViewController: UIViewController, GIDSignInUIDelegate {

    override func viewDidLoad() {
        super.viewDidLoad()


        setupGoogleButtons()
    }

    fileprivate func setupGoogleButtons() {
        //add google sign in button
        let googleButton = GIDSignInButton()
        googleButton.frame = CGRect(x: 16, y: 116 + 66, width: view.frame.width - 32, height: 50)
        view.addSubview(googleButton)
        //custom google button
        let customButton = UIButton(type: .system)
        customButton.frame = CGRect(x: 16, y: 116 + 66 + 66, width: view.frame.width - 32, height: 50)
        customButton.backgroundColor = .orange
        customButton.setTitle("Custom Google Sign In", for: .normal)
        customButton.addTarget(self, action: #selector(handleCustomGoogleSign), for: .touchUpInside)
        customButton.setTitleColor(.white, for: .normal)
        customButton.titleLabel?.font = UIFont.boldSystemFont(ofSize: 14)
        view.addSubview(customButton)

        GIDSignIn.sharedInstance().uiDelegate = self
    }

    func handleCustomGoogleSign() {
        GIDSignIn.sharedInstance().signIn()
    }

    override func didReceiveMemoryWarning() {
        super.didReceiveMemoryWarning()
        // Dispose of any resources that can be recreated.
    }


}

这也是我的 main.storyboard 的截图:

Main.Storyboard Screenshot

【问题讨论】:

    标签: swift xcode uiviewcontroller uikit google-signin


    【解决方案1】:

    应用程序没有从故事板中指定的InitialViewController 开始的原因是因为这个window!.rootViewController = tabBarVC。通过在window 上设置rootViewController,您实际上会覆盖在storyboard 上设置的规范。因此它被忽略了。

    如果您想在屏幕截图中显示带有TheOracle 标签的ViewController,您应该给ViewController 一个标识符。让我们称之为OnboardingViewController

    那么你应该更新这个:

    if let tabBarVC = sb.instantiateViewController(withIdentifier: "LogInViewController") as? ViewController {
        window!.rootViewController = tabBarVC
    }
    

    if let tabBarVC = sb.instantiateViewController(withIdentifier: "OnboardingViewController") as? ViewController {
        window!.rootViewController = tabBarVC
    }
    

    【讨论】:

    • 谢谢,这个解决方案有效!我想知道您是否也可以查看我的最新问题。我有一个奇怪的问题。真的很感激!
    • 嗨,你有没有机会看看我最近的问题。非常感谢任何帮助。
    • 它对你有用。关于你最近提出的问题,其实我还没看。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2014-04-28
    相关资源
    最近更新 更多