-1

i want to use a data between views but i have a tab bar view control. Actually my initial view login view i get "access_token" as a sting and if i can successfully login pass tab bar control's first view but i want to use access token string in second tab bar view for get another data from webservice. how can i get acces token string in tab bar controller's second view?

self.strSuccessful = json["acces_token"] as? String

my second view in tab bar view controller

class SalesVC: UIViewController {
   var strAccesToken: String!
  }

my initial (first view controller not inculude tabbar because login screen)

override func prepare(for segue: UIStoryboardSegue, sender: Any?) {

    let salesViewController = segue.destination as! SalesVC

    salesViewController.strAccessToken = strSuccessfulAccesToken


}
C. Sayin
  • 1
  • 7

3 Answers3

0

The simplest solution is you can use UserDefaults and store your access token in it in LoginController after that in SecondTabController retrieve access token from user defaults.

Save access token in UserDefaults

UserDefaults.standard.set(strSuccessfulAccesToken, forKey: "AccessToken")

Now in SecondTabController retrieve access token from user defaults.

if let accessToken =  UserDefaults.standard.string(forKey: "AccessToken") {
    print(accessToken)
}

If you want to manually passed the access token from LoginController then set access token in prepareForSegue of LoginController like this way.

override func prepare(for segue: UIStoryboardSegue, sender: Any?) {
    let tabbar = segue.destination as! UITabBarController
    if let salesVC = tabbar.viewControllers[1] as? SalesVC {
        salesVC.strAccessToken = strSuccessfulAccesToken
    }
}
Nirav D
  • 71,513
  • 12
  • 161
  • 183
0

In general case you can use UserDefaults to store simple data (like UI preference, for example), but you have mentioned access token, and if you want to protect user account, you should be careful and choose secure storage like Keychain.

Remember, UserDefaults is just simple XML file and anybody with access to device can read it.

This is good tutorial about secure iOS User Data

Sergey Gamayunov
  • 658
  • 7
  • 15
0

I think that you are asking how to transfer data between different view controllers within a tab bar controller. This is how I've done it:

Define the tab bar class and variable to hold the data:

class ItemTabBarController: UITabBarController {
    var allTags: [TagData] = []

    override func viewDidLoad() {
        print("ItemTabBarController.viewDidLoad")
    }
}

Make sure that the custom class property for the tab bar in the storyboard is "ItemTabBarController"

In each view controller that wants to set a value in the tab bar:

func configureItemTabBarController() {
        let itemTabBarController = self.tabBarController  as! ItemTabBarController
        self.viewModel.allTags = viewModel.configureAllTags()
        itemTabBarController.allTags = self.viewModel.allTags
    }
user6902806
  • 280
  • 1
  • 12