在集成谷歌登錄單獨的登錄頁面后,我正在嘗試導航到我的主要內容。在創建登錄頁面之前,我的應用程式的入口點是導航控制器。現在我需要用戶在此之前登錄,所以我添加了一個帶有谷歌登錄按鈕的視圖控制器,并且登錄作業正常。這是我的故事板:

現在,如果登錄成功,我希望顯示應用程式的主要內容視圖(從現在開始就好像入口點是導航控制器)。我嘗試了以下方法:
@IBAction func signIn(_ sender: Any) {
let signInConfig = GIDConfiguration.init(clientID: "xyz.apps.googleusercontent.com")
GIDSignIn.sharedInstance.signIn(with: signInConfig, presenting: self) { user, error in
guard error == nil else { return }
// If sign in succeeded, display the app's main content View.
let vc = self.storyboard?.instantiateViewController(withIdentifier: "scanReceiptsViewController") as! ViewController
self.navigationController?.pushViewController(vc, animated: true)
self.present(vc, animated: true, completion: nil)
}
}
有2個問題:
- 新的視圖控制器以模態方式呈現,用戶可以非常輕松地回傳登錄頁面,它會分散我的應用程式的流程
- 用戶每次都需要登錄才能導航到主內容視圖控制器,我希望如果登錄入口點從現在開始成為主故事板。如果這有意義
謝謝,對不起,我是快速學習的新手!
uj5u.com熱心網友回復:
我建議您將導航控制器(從現在起將其稱為ScanRecipientsViewController)作為默認的應用程式入口點,因為用戶只需要在未登錄的情況下查看登錄螢屏,這在大多數情況下是不需要的案子。這將使我們在用戶登錄時在應用程式啟動時獲得稍微更快和更流暢的性能。
您可以在 AppDelegate 的func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplication.LaunchOptionsKey: Any]?) -> Bool方法中處理這種情況。例如,檢查用戶是否登錄,如果沒有,請將您的設定LoginController為新的根控制器,這將替換您ScanRecipientsViewController在情節提要中設定為根的控制器。
func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplication.LaunchOptionsKey: Any]?) -> Bool {
if GIDSignIn.sharedInstance.currentUser == nil { // just a guess, please refer to the documentation on how to check if the user is logged in
let storyboard = UIStoryboard(name: "Login", bundle: nil)
/*
If you use the SceneDelegate in your project, you will need to retrieve the window object from it instead of from the AppDelegate.
let window = (UIApplication.shared.connectedScenes.first?.delegate as? SceneDelegate)?.window
*/
window?.rootViewController = storyboard.instantiateViewController(withIdentifier: "LoginController")
}
return true
}
用戶成功登錄后,您可以創建您的新實體ScanRecipientsViewController并將其再次設定為根控制器。您可以通過例如在您的AppDelegate:
extension AppDelegate {
func openRecipientsController() {
let storyboard = UIStoryboard(name: "Home", bundle: nil)
/*
If you use the SceneDelegate in your project, you will need to retrieve the window object from it instead of from the AppDelegate.
let window = (UIApplication.shared.connectedScenes.first?.delegate as? SceneDelegate)?.window
*/
window?.rootViewController = storyboard.instantiateInitialViewController()
}
}
在你的LoginViewController:
@IBAction func signIn(_ sender: Any) {
let signInConfig = GIDConfiguration.init(clientID: "xyz.apps.googleusercontent.com")
GIDSignIn.sharedInstance.signIn(with: signInConfig, presenting: self) { user, error in
guard error == nil else { return }
// If sign in succeeded, display the app's main content View.
(UIApplication.shared.delegate as? AppDelegate)?.openRecipientsController()
}
}
最后,您通常不會同時使用推送和呈現,您需要在兩個選項中選擇一個。
let vc = self.storyboard?.instantiateViewController(withIdentifier: "scanReceiptsViewController") as! ViewController
self.navigationController?.pushViewController(vc, animated: true)
self.present(vc, animated: true, completion: nil)
祝您在使用 Swift 學習 iOS 開發程序中獲得樂趣!
轉載請註明出處,本文鏈接:https://www.uj5u.com/qianduan/493461.html
