2016-06-29 52 views
0

我已經添加谷歌登錄到我的應用程序正在工作,但我似乎無法讓我的觀點改變以編程方式成功登錄。更改視圖谷歌登錄ios

這是我的演示故事板。灰色框是登錄按鈕,我已經添加了一個按鈕「手動移動頁面」,從登錄屏幕切換到標籤欄控制器屏幕。點擊此按預期工作,我需要在應用程序檢測到用戶登錄時自動發生。

當用戶打開應用程序時,如果用戶登錄,第一個屏幕是視圖控制器「登錄屏幕」通過下面的代碼檢查我想將它們轉發到主屏幕(第一個標籤)。請讓我知道是否有更好/更快的方式做到這一點,如果可能的話。

Storyboard

Loginscreen:

override func viewDidLoad() { 
    super.viewDidLoad() 

    // Google sign in 
    GIDSignIn.sharedInstance().uiDelegate = self 
    // Check if they're already signed in 
    GIDSignIn.sharedInstance().signInSilently() 

    // check if the user is signed in 
    if (GIDSignIn.sharedInstance().hasAuthInKeychain()){ 
     print("signed in") 
     // Forward the user here straight away... 
    } else { 
     print ("not signed in") 
     // Need to handle the forwarding once they sign in. 
    } 
} 

回答

1

您可以從您的登錄屏幕任何你想要的ViewController添加Segue和編寫代碼如下所示:

if (GIDSignIn.sharedInstance().hasAuthInKeychain()){ 
     print("signed in") 
    performSegueWithIdentifier("your_segue_name", sender: self) 
     // Forward the user here straight away... 
    } 

或者你可以通過模態呈現ViewController

if (GIDSignIn.sharedInstance().hasAuthInKeychain()){ 
let storyboard = UIStoryboard(name: "Yourstoryboardname(Example: Main)", bundle: nil) 
let vc = storyboard.instantiateViewControllerWithIdentifier("yourviewcontrollertobepresented") as! UIViewController 
self.presentViewController(vc, animated: true, completion: nil) 
} 

您需要姓名您想要出席的viewcontroller。爲此,您單擊故事板中的ViewController並在Identity inspector中找到Storyboard ID。將其命名爲instantiateViewControllerWithIdentifier("yourviewcontrollertobepresented")

+0

非常感謝,第二個選項正常工作!雖然每次谷歌嘗試登錄時我都必須將它放入通知偵聽器,但在用戶電子郵件被檢索導致失敗之前,它需要一兩秒鐘的時間導致頁面切換。我不確定加速Google登錄的推薦方式是什麼,可能會在用戶登錄後緩存詳細信息。 – burg93