2014-03-03 37 views
0

在我的AppDelegate我檢查,如果已經有登錄的用戶。在iOS應用程序中進行用戶身份驗證後,繼續流動的最佳做法是什麼?

if (userExists) { 
     // Set my main view as the root view controller. 
     UINavigationController *navController = [[UINavigationController alloc] init]; 
     MainViewController *mainVC = [[MainViewController alloc] init]; 
     [navController setRootViewController:mainVC animated:YES]; 
     [self.window setRootViewController:navController]; 
} 

如果沒有現有用戶我提出一個登錄界面。

現在,一旦用戶已經登錄,我必須繼續流程,顯示MainViewController

我目前在做以下,塊,當用戶認證成功時調用:

UINavigationController *navController = [[UINavigationController alloc] init]; 
MainViewController *mainVC = [[MainViewController alloc] init]; 
[navController setRootViewController:wordViewController animated:YES]; 
UIApplication *application = [UIApplication sharedApplication]; 
[application.windows.firstObject setRootViewController:navController]; 

這是一個很好的方式去?由於這是整個iOS應用程序中頻繁重複的模式,因此最常見,最明智的做法是什麼?

+0

的可能重複的[用於故事板登錄屏幕的最佳實踐中,在註銷數據處理結算](http://stackoverflow.com/questions/19962276/best-practices-for-storyboard-login- screen-handling-clearing-of-data-upon-logou) – vokilam

回答

0

當用戶通過身份驗證時,您可以重構繼續流的代碼。您可以立即或成功登錄後調用它。例如:

- (void)showMainScreenForUser:(UserType *)user 
{ 
     UINavigationController *navController = [[UINavigationController alloc] init]; 
     MainViewController *mainVC = [[MainViewController alloc] initWithUser:user]; 
     [navController setRootViewController:mainVC animated:YES]; 
     [self.window setRootViewController:navController]; 
} 
+0

我認爲這應該放在我的AppDelegate中,對吧? – ayberkt

+0

是的,因爲你已經有了那個邏輯。 – Macondo2Seattle

相關問題