Passing data from completion block to view controller swift 3 - ios

!I have been stuck for 2 days on this issue. Here is my scenario.
User clicks login button, if email address and password are stored in UserDefaults, the app silently login the user in, first presenting an UIAlertController while retrieving the user's data from the server. Once the data is returned to the app, a completion handler assigns a local property var User: [String: AnyObject] the results. I can print(self.User) at this point and the data is properly assigned. All good. The problem occur when I try to pass this data to the next controller that I am presenting via present. Here is my code:
LoginViewController.swift
-----
dismiss(animated:true, completion: {
let tenantViewController = storyboard.instantiateViewController(withIdentifier :"tenantDashboardViewController") as! TenantDashboardViewController
tenantViewController.User = user
print(tenantViewController.User) //Works
self.present(tenantViewController, animated: true)
})
Here is my destination viewcontroller
import Foundation
import SideMenuController
class TenantDashboardViewController: SideMenuController {
var User: [String: AnyObject]?
override func viewDidLoad() {
super.viewDidLoad()
performSegue(withIdentifier: "showTenantDashboardHomeViewController", sender: nil)
performSegue(withIdentifier: "containTenantSideMenu", sender: nil)
print(self.User!) //Always returns nil, crashes app
}
}
Thanks in advance!

If you're storing a user object app-side, you should store the User Object in NSUserDefaults. Make it NSCoding compliant so you can encode/decode the User class as you need to.
Then, as Matthew states, store your username/password combo in Keychain, because it is extremely sensitive and Keychain is designed to protect your users' credentials in a secure way.

Related

Persisting data in iOS app

I am new to iOS. I want to save authentication token recieved from a REST API to use in further api calls without requiring a login. Currently I am using UserDefaults to store this token. This token works fine unless app is completely closed. Relaunching the app again takes me to login screen.
Saving the token like this
UserDefaults.standard.setValue(authToken, forKey: "auth_token")
UserDefaults.standard.synchronize() // Now this call is derpecated. Framework handles this call at proper places.
LoginViewController
override func viewDidLoad(){
super.viewDidLoad()
if UserDefaults.standard.string(forKey: "auth_token") != nil {
self.performSegue(withIdentifier: "login_success", sender: self)
}
}
But the issue is how can I persist this token even after the app was completely closed ?
EDIT
I have also tried syncing UserDefaults within applicationWillTerminate methods of AppDelegate class just to make sure but that even doesn't work.
Nothing wrong with the UserDefaults. Just wrapping the triggering segue call in main queue was required to properly work.
override func viewDidLoad() {
super.viewDidLoad()
if UserDefaults.standard.string(forKey: "auth_token") != nil {
DispatchQueue.main.async {
self.performSegue(withIdentifier: "login_success", sender: self)
}
}
}

Accessing UserDefaults in Swift from other viewControllers

In my application, I use UserDefaults to store the user's login status (whether they are logged in or not), and their username. It works fine in that when I login, close the app, and open it again my app skips the login page and recognizes that I am already logged in. Although, I am now trying to install a logout button to a separate viewController. When clicked, this logout button needs to 1.) Reset UserDefaults.loginStatus to "False" 2.) Reset UserDefaults.username to nil 3.) Perform a segue to the login page.
Here is the related code from my ViewController.swift file. This is the first viewController which controls the loginPage.
import UIKit
import Firebase
let defaults = UserDefaults.standard
class ViewController: UIViewController {
func DoLogin(username: String, password: String) {
//I Am not including a lot of the other stuff that takes place in this function, only the part that involves the defaults global variable
defaults.setValue(username, forKey: "username")
defaults.setValue("true", forKey: "loginStatus")
defaults.synchronize()
self.performSegue(withIdentifier: "loginToMain", sender: self) //This takes them to the main page of the app
}
override func viewDidLoad() {
super.viewDidLoad()
if let stringOne = defaults.string(forKey: "loginStatus") {
if stringOne == "true" { //If the user is logged in, proceed to main screen
DispatchQueue.main.async
{
self.performSegue(withIdentifier: "loginToMain", sender: self)
}
}
}
}
Below is my code in SecondViewController.swift, particularly the logout function.
import UIKit
import Firebase
class SecondViewController: UIViewController {
override func viewDidLoad() {
super.viewDidLoad()
if let username = defaults.string(forKey: "username") {
checkAppSetup(username: username) //This is an unrelated function
//I included this because this works fine. Proving that I am able to read the defaults variable fine from this other viewController
}
}
#IBAction func logout(_ sender: Any) {
defaults.setValue("false", forKey: "username")
defaults.setValue("false", forKey: "loginStatus")
defaults.synchronize()
performSegue(withIdentifier: "logoutSegue", sender: nil)
}
When the logout function is run, the segue performs fine but the default values do not change. Can someone explain why and what I can do to get around this?
**Side note, I am not actually going to set the defaults to "false" and "false". That is just temporary for while I am debugging this issue.
Several things.
You should be using set(_:forKey:) and object(_:forKey) to read and write key/value pairs to defaults, not setValue(_:forKey). (Your use of defaults.string(forKey: "loginStatus") is correct, however.)
You should probably be writing a nil to the userName key:
defaults.set(nil, forKey: "username")
And your logout IBAction should almost certainly be setting loginStatus to false, not true.
Try changing those things.
Also, there is no reason to call synchronize unless you are terminating your app in Xcode rather than pressing the home button on the device/simulator in order to let it exit normally.
Hey i used the exactly same concept recently :
1) In your initial view, in the viewDidLoad() , check whether somebody is already logged in or not, and only one user can be logged in one device at a time, so we check like
let defaults = UserDefaults.standard
if defaults.object(forKey: "userName") != nil && defaults.object(forKey: "userPassword") != nil
{
let loginObject = self.storyboard?.instantiateViewController(withIdentifier: "YourSecondViewController") as! YourSecondViewController
//As someone's details are already saved so we auto-login and move to second view
}}
2) In your sign in button function , check whatever condition you want to check and then, inside the same, if condition satisfies then save data to userDefaults.
// If no details are saved in defaults, then control will come to this part, where we will save the entered userName and Password
let defaults = UserDefaults.standard
defaults.set(self.enteredUseName, forKey: "userName")
defaults.set(self.enteredPassword, forKey: "Password")
defaults.synchronize()
3) On logout button , delete the userDefaults and load the login view again :
let defaults = UserDefaults.standard
defaults.removeObject(forKey: "userName") //We Will delete the userDefaults
defaults.removeObject(forKey: "userPassword")
defaults.synchronize() //Sync. the defaults.
navigationController?.popToRootViewController(animated: true) //Move Back to initial view.
4) If you are using a navigation control, that you must be using :P then you will surely see the back button which will open the second view if clicked, for that you can hide the navigation bar in viewDidLoad() of your login view
self.navigationController?.navigationBar.isHidden = true

Starting at a certain view if user is logged in iOS Swift

I am currently making a small login app using Firebase. I am currently having problems with my login page.
When my users already are logged in, when they open the app, I want to change the initial view controller, so that the user can go straight to the homepage.
So my question is, what line of code do I have to perform in order to do this?
override func viewDidLoad() {
super.viewDidLoad()
if FIRAuth.auth() ? .currentUser ? .uid == nil {
notLoggedIn()
}
}
func notLoggedIn() {
let storyBoard: UIStoryboard = UIStoryboard(name: "Main", bundle: nil)
let nextViewController = storyBoard.instantiateViewController(withIdentifier: "Startpage") as!ViewController
self.present(nextViewController, animated: true, completion: nil)
}
There's a couple of ways you can do this. If you really want to change the initial view controller, you would want to NOT set an initial view controller in your storyboard, then in your app delegate's application(_:didFinishLaunchingWithOptions:) implementation, you would create a new Window object and set whichever view controller on it you want to present as the rootViewController. Then, you would call makeKeyAndVisible on that window object. Note that if you do it this way, you'll have to separately handle the case when they log out if you want to display your login window again. In that case you would just do the same thing again: make a new window object with your new ViewController object as the rootViewController and present it.
Another option is to check if they are logged in in your initial view controller's viewDidLoad method and then present your login screen if they aren't. This is what I do in one of applications where the app needs some data, either by logging into an account or manually adding it, before it can do anything.
EDIT:
Here's what my viewDidLoad, etc. looks like (note that mine project is in Objective-C, so I'm just kinda guessing without actually testing it what the correct Swift syntax is. You might need to make some adjustments) You have to dispatch the present call to the main queue because in viewDidLoad you (probably) don't have everything in order yet to actually present a new view controller (I did this quite a long time ago, so I don't recall exactly why it has to be dispatched, but because of the fact that we're already in the process of presenting the current view controller, it makes sense that you wouldn't be able to present another one at the same time. Maybe someone else can weigh in on this, because I really don't remember anymore.):
override func viewDidLoad() {
super.viewDidLoad()
if (!userLoggedIn) {
showLoginScreen()
}
}
func showLoginScreen() {
let loginViewController = storyboard?.instantiateViewController(withIdentifier: "Startpage") as! ViewController
DispatchQueue.main.async {
present(loginViewController, animated: true, completion: nil)
}
}
You can use this line of codes.
Keep in mind that you should add storyboard reference with identifier
named respectively for your need - goToLogin - in my case.
Hope It'll be helpful for anyone.
override func viewDidLoad() {
super.viewDidLoad()
Auth.auth().addStateDidChangeListener { auth, user in
if let user = user {
// User is signed in.
print("user signed in")
//Add the rest of the code here because after passig the caluses
// viewdidload will call another funxtions to it can crash
} else {
// User not signed in
self.performSegue(withIdentifier: "goToLogin", sender: Any?.self)
}
}
}

iOS Swift: best way to pass data to other VCs after query is completed

Context: iOS App written in Swift 3 powered by Firebase 3.0
Challenge: On my app, the user's currentScore is stored on Firebase. This user can complete/un-complete tasks (that will increase/decrease its currentScore) from several ViewControllers.
Overview of the App's architecture:
ProfileVC - where I fetch the currentUser's data from Firebase & display the currentScore.
⎿ ContainerView
⎿ CollectionViewController - users can update their score from here
⎿ DetailsVC - (when users tap on a collectionView cell) - again users can update their score from here.
Question: I need to pass the currentScore to the VCs where the score can be updated. I thought about using prepare(forSegue) in cascade but this doesn't work since it passes "nil" before the query on ProfileVC is finished.
I want to avoid having a global variable as I've been told it's bad practice.
Any thoughts would be much appreciated.
Why don't you create a function that will pull all data before you do anything else.
So in ViewDidLoad call...
pullFirebaseDataASYNC()
Function will look like below...
typealias CompletionHandler = (_ success: Bool) -> Void
func pullFirebaseDataASYNC() {
self.pullFirebaseDataFunction() { (success) -> Void in
if success {
// Perform all other functions and carry on as normal...
Firebase function may look like...
func pullFirebaseDataFunction(completionHandler: #escaping CompletionHandler) {
let refUserData = DBProvider.instance.userDataRef
refUserData.observeSingleEvent(of: .value, with: { snapshot in
if let dictionary = snapshot.value as? [String: AnyObject] {
self.userCurrentScore = dictionary["UserScore"] as! Int
completionHandler(true)
}
})
}
Then when you segue the information across...
In ProfileVC
Create 2 properties
var containerVC: ContainerVC!
var userCurrentScore = Int()
Include the below function in ProfileVC...
override func prepare(for segue: UIStoryboardSegue, sender: Any?) {
if segue.identifier == "ProfileToContainerSegue" {
let destination = segue.destination as! ContainerVC
containerVC = destination
containerVC.userCurrentScore = userCurrentScore
}
}
In ContainerVC create a property
var userCurrentScore = Int()
Ways to improve could be an error message to make sure all the information is pulled from Firebase before the user can continue...
Then the information can be segued across the same way as above.
Try instantiation, first embed a navigation controller to your first storyboard, and then give a storyboardID to the VC you are going to show.
let feedVCScene = self.navigationController?.storyboard?.instantiateViewController(withIdentifier: "ViewControllerVC_ID") as! ViewController
feedVCScene.scoreToChange = current_Score // scoreToChange is your local variable in the class
// current_Score is the current score of the user.
self.navigationController?.pushViewController(feedVCScene, animated: true)
PS:- The reason why instantiation is much healthier than a modal segue storyboard transfer , it nearly removes the memory leaks that you have while navigating to and fro, also avoid's stacking of the VC's.

How can I continue to receive messages/data from apple watch after dismissing a secondary view contoller

I am writing an application that allows me to record my golf strokes and save them, so that I can view the scores on an iPhone. The problem is that while the app does receive messages after launching and opening secondary view controllers, it stops listening for messages after dismissing any secondary view controller. If I then switch to a secondary view controller, it will listen for messages until I dismiss it again. I do not know why the root View controller will only listen until a secondary view controller is opened.
I am using sendMessage and its corresponding receiver function to transfer the number of stokes between devices. Each view controller contains the receive message function.
The code in question is as follows:
import UIKit // or WatchKit on Apple watch
import WatchConnectivity
class ViewController: UIViewController, WCSessionDelegate {
var session: WCSession!
let userData = NSUserDefaults.standardUserDefaults()
override func viewDidLoad() {
super.viewDidLoad()
// Do any additional setup after loading the view, typically from a nib.
ScrollHoles.contentSize = CGSize(width: 360,height: 1090)
if (WCSession.isSupported()){
self.session = WCSession.defaultSession()
self.session.delegate = self
self.session.activateSession()
userData.synchronize()
}
}
The send message function is...
// Save Data From Strokes To standatdUserDefaults.
#IBAction func SaveButton(sender: AnyObject) {
let hole1Score = Strokes1.text;
userData.setObject(hole1Score, forKey: "hole1Strokes")
userData.synchronize()
session.sendMessage(["score1A": hole1Score!], replyHandler: nil, errorHandler: nil) // score1A becomes score1B on Apple watch
}
and the receive message function is...
// Recieve Data from Apple Watch And Save It To standardUserDefaults (e.g. # of strokes).
func session(session: WCSession, didReceiveMessage message: [String : AnyObject]) {
let hole1Msg = message["score1B"] as? String // score1B becomes score1A on apple watch
let hole2Msg = message["score2B"] as? String
if (hole1Msg != nil){
userData.setObject(hole1Msg, forKey: "hole1Strokes")
}
if (hole2Msg != nil){
userData.setObject(hole2Msg, forKey: "hole2Strokes")
}
The constants and if statement continue for all 18 holes.
I am using the following to dismiss secondary view controllers.
#IBAction func backButton(sender: AnyObject) {
self.dismissViewControllerAnimated(true, completion: {});
}
As explained in this answer, WCSession only supports a single delegate at a time.
Since you're setting up your session within view controllers, the session delegate is changing when you load another view controller. Although you may dismiss the secondary controller, it is still the delegate for the session.
This is why your root view controller no longer receives messages, as it is no longer the session delegate.
Recommended approach by Apple engineers:
As the previous answer and developer forum posts suggest, you can create a session wrapper which you activate in the phone's AppDelegate, have it handle app-wide messages from your watch, and then either update a data store, or directly pass that data on to observers.
In general, it's advantageous to setup Watch Connectivity as soon as possible (at launch, instead of in a specific view controller).
Off-site tutorial:
Natasha The Robot has an excellent tutorial which walks through using a Watch Connectivity session manager and data source in great detail.

Resources