Reputation: 391
When a user authenticates to Google, I want to record the user's status somewhere. When a user clicks on a cell, I want that if the user is authenticated, then I want to send him to another View, and if not, then send him to the authorization page. But I can't do it. I created a flag, but for some reason it only fires after restarting the application.
...
protocol SourceViewControllerDelegate{
func requestReloadTable()
}
class SourcesViewController: UIViewController, GIDSignInDelegate {
...
var isAuthenticationComplete: GIDAuthentication?
...
func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
let cell = tableView.dequeueReusableCell(withIdentifier: SourceCell.reuseId, for: indexPath) as! SourceCell
cell.sourceViewControllerDelegate = self
....
return cell
}
func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
tableView.deselectRow(at: indexPath, animated: true)
if isAuthenticationComplete != nil {
let storyboard = UIStoryboard(name: "GoogleDriveViewController", bundle: nil)
let vc = storyboard.instantiateViewController(identifier: "GoogleDrive") as! GoogleDriveViewController
navigationController?.pushViewController(vc, animated: true)
} else {
GIDSignIn.sharedInstance().signIn()
}
}
...
func sign(_ signIn: GIDSignIn!, didSignInFor user: GIDGoogleUser!,
withError error: Error!) {
if let error = error {
print(error.localizedDescription)
return
}
guard let authentication = user.authentication else { return }
isAuthenticationComplete = authentication
}
...
}
extension SourcesViewController: SourceViewControllerDelegate {
func requestReloadTable() {
tableView.reloadData()
}
}
...
class SourceCell: UITableViewCell {
...
var sourceViewControllerDelegate: SourceViewControllerDelegate?
...
@IBAction func logInLogOutButton(_ sender: Any) {
print("Log Out button pressed")
GIDSignIn.sharedInstance().signOut()
sourceViewController?.isAuthenticationComplete = nil
sourceViewControllerDelegate?.requestReloadTable()
}
}
After I click on this button, I can still get to the other screen, but if I restart the application, then when I click on this button I will be taken to the authorization page.
Upvotes: 0
Views: 66
Reputation: 100523
This
var isAuthenticationComplete: GIDAuthentication?
is an instance variable not a saved 1 meaning it's value is nil for the every new object creation of SourcesViewController
, you need to save your app state in bool user defaults and check it every open
Save
guard let authentication = user.authentication else { return }
isAuthenticationComplete = authentication
Userdefaults.standard.set(true,forKey:"userAuthorized")
Check
if Userdefaults.standard.bool(forKey:"userAuthorized") { }
Upvotes: 1