This is my first iOS app and at this moment I'm struggling with Acccount / Login transitions on my storyboard.
What I'm trying to do is, If there is no logged user, display the login, otherwise display the account menu (Edit profile, My savings, and Logout).
Actually I'm achieving with this code on my AccountMenuViewController:
- (void)viewDidLoad
{
[super viewDidLoad];
if (![self isLogged]) {
[self presentLoginView];
}
}
#pragma mark - Login methods
- (BOOL)isLogged
{
NSUserDefaults *defaults = [NSUserDefaults standardUserDefaults];
return [[defaults objectForKey:#"loggedIn"] boolValue];
}
- (void)presentLoginView
{
[self performSegueWithIdentifier:#"LoginSegueId" sender:self];
}
When the user touch "Cancel login" I'm trying to redirect to my MainView, I've make a segue but the Tab Bar is not displayed.
And when the login is ok, Close the login view.
I want to know your advices and tips, What are the best practices with this scenario?
This is my Storyboard.
First you should either make the login screen your rootViewController and just push to main view if
[self isLogged]==YES
or have the Login presented in a model fashion with the main view as a root, that way instead of a segue you can just dismiss it and the main view will be present underneath. you can also have the main view push the login and just hide the nav bar and use 'cancel login' as a sort of '< back' button that you would see on the nav bar by using the popViewController method..... also you can simplify the method:
- (BOOL)isLogged
{
NSUserDefaults *defaults = [NSUserDefaults standardUserDefaults];
return [[defaults objectForKey:#"loggedIn"] boolValue];
}
can be :
- (BOOL)isLogged
{
NSUserDefaults *defaults = [NSUserDefaults standardUserDefaults];
return [[NSUserDefaults standardUserDefaults] boolForKey:#"loggedIn"];
}
hope i helped!
Related
I have set up a Tab Bar that controls some view controllers in my app. In my view controller CheckViewController, I have user entry-based data that I would like to save every time I am switching from CheckViewController to another view controller. The code I have currently only saves data the first time I switch from CheckViewController to another view controller using the tab bar. I would like to save data every single time CheckViewController is opened and switched to another view controller.
I am currently using:
- (void)viewDidLoad {
[super viewDidLoad];
self.tabBarController.delegate = self;
//code
}
- (BOOL)tabBarController:(UITabBarController *)tabBarController shouldSelectViewController:(UIViewController *)viewController {
if (![viewController isEqual:self]) {
if ([self.tabBarController.selectedViewController isEqual:self] ) {
//save data
}
}
return YES;
}
Suggestions are appreciated. Thanks!
for saving small data,you can use NSUserDefault.
here is an example for saving and reading data.
Save:
[[NSUserDefaults standardUserDefaults] setObject:aData forKey:aKey];
[[NSUserDefaults standardUserDefaults] synchronize];
Read:
[[NSUserDefaults standardUserDefaults] objectForKey:aKey];
Hi im currently developing an app where the initial view controller is a screen with two buttons, one is for one thing and the other is for a nother thing. Each button leads to a new viewcontroller. What i want to do is for the app to recognize the users choice of button and the save that so when the user opens the app the next time it goes straight to the new viewcontroller.
I know i have to use NSUserDefaults but im pretty new to coding so if you guys could explain it it detail that would be perfect.
Thanks!
When clicks the button:
- (void) buttonPressed: (id) sender
{
NSUserDefaults *userDefaults = [NSUserDefaults standardUserDefaults];
[userDefaults setObject:#"Something" forKey:#"SOME_KEY"];
[userDefaults synchronize];
}
You can add this code to handle what you want:
- (void)viewWillAppear:(BOOL)animated
{
NSUserDefaults *userDefaults = [NSUserDefaults standardUserDefaults];
if ([userDefaults objectForKey:#"SOME_KEY"] != nil)
{
if ([[userDefaults objectForKey:#"SOME_KEY"] isEqualToString:#"Something"])
{
// push one ViewController
}
else
{
// push the other one
}
}
}
i have created a settingsView and also a view that gives you a choice to be notified if you want at a certain time its a yes or no but i do not know how to access the value of the uiswitch i know instead of trying to get the views object i should try the preference that the state has been saved to but i don't know how i just want to tap my UISwitch and then in the settingsView the UILabel gets filled but i don't know how to access the value on the UISwitch once tapped i have saved it in NSUserDefaults in the notifiedView but do not know how to access that value in the settingsView so i can put in the settingsView
if (UISwitch.on)
{
//notify me please
} else {
// leave as is
}
of course that's just a quick layout of how i want it to be though and also where do i put the it in the settingsView viewDidLoad, viewWillAppear or both please help im a new be as well so please be kind if i have said things that you would deem not the right syntax thanks
Try this code:
// add this code in your switch touch event
- (IBAction)YourSwitch:(UISwitch*)sender
{
if (UISwitch.on)
{
NSUserDefaults *defaults = [NSUserDefaults standardUserDefaults];
[defaults setBool:true forKey:#"Sound"];
[defaults synchronize];
}
else
{
NSUserDefaults *defaults = [NSUserDefaults standardUserDefaults];
[defaults setBool:false forKey:#"Sound"];
[defaults synchronize];
}
}
Add this code in your viewdidload .
BOOL swithState = [[NSUserDefaults standardUserDefaults] boolForKey:#"Sound"];
if (swithState)
{
_ref_slider.on=true;
}
else
{
_ref_slider.on=false;
}
i hope this code is useful for you.
Just make a UISwitch property in your UIViewController and make sure you set it in the init/viewDidLoad of your class (from an IBOutlet or when you programmatically add it to your view). Then you can access the on-property of the UISwitch from anywhere within your class (and a load of extra features!) and use the if-statement as you want..
Apple Doc ref: https://developer.apple.com/library/ios/documentation/uikit/reference/UISwitch_Class/Reference/Reference.html
If your are using storyboard then hook up your UISwitch's action and write the below code in it.
- (IBAction)switchTapped:(id)sender
{
if ([sender isOn]) {
// your code
}
else
{
// your code
}
}
In my app, I created a first ViewController (in storyboard) called welcome, with all the instructions. At the end of the page, I wanted to insert a box that could be selected showing "don't show again". By clicking on this box, and going to the next page, the first viewController will vanish. When I reopen the app, the controller won't be shown again. The only way to show it is by going on settings and selecting the switch "repristinate original settings" or something like that. Please, can anyone help me? Thanks!
I found an example but is not what I want:
- (IBAction)leggi{
NSString *stringaTesto = campo.text;
testo.text = [[NSString alloc] initWithFormat:#"%#", stringaTesto];
NSString *testoInserito = testo.text;
NSUserDefaults *defaults = [NSUserDefaults standardUserDefaults];
[defaults setObject:testoInserito forKey:#"ciao"];
[defaults synchronize];
}
- (void)viewDidLoad
{
[super viewDidLoad];
NSString *testoSalvato = [[NSUserDefaults standardUserDefaults] objectForKey:#"ciao"];
if (testoSalvato == nil) {
testo.text = #"Non hai ancora inserito il tuo nome";
} else {
testo.text = [[NSString alloc] initWithFormat:#"Ciao %#", testoSalvato];
}
}
The IB Outlet is linked to a button
store the don't show again as a BOOL in NSUserDefaults.. and check it before showing the view..
if it is TRUE..dont show.other wise show it.
edit
Lets say you have your app delegate and currently show your don't show view again (Lets say A) from it ..after which you show another view(Lets say B)
then in your app delegate you have to get a BOOL
like this
BOOL _Dont_Show_Again = [NSUSerDefaults standardDefaults] boolForkey : #"Don't Show"];
if(_Dont_Show_Again)
{
load B Code here...
}
else
{
load A Code here;
}
first time _Dont_Show_Again will be 0 since it does not exist in default..but if user select don't show you should save it in the default and this code will then work fine for you
I've tabbar controller with 6 to 7 tabs on it with customization allowed (means that the user can change the order of tab bar display). And tab 1,3,5,7 requires the user to login access before seeing any contents on the view.
I don’t know as to how to identify which tabbar requires login access (Can we create #protocal or something). And after the user logs in successfully. I don’t know how to select the last tabbar, which the user has touched on.
I was using appdelegate by confiming to UITabBarControllerDelegate in AppDelegate
-(BOOL)tabBarController:(UITabBarController *)aTabBar shouldSelectViewController:(UIViewController *)viewController {
NSUserDefaults *standardUserDefaults = [NSUserDefaults standardUserDefaults];
BOOL hasValidLogin = [standardUserDefaults boolForKey:#"hasValidLogin"];
if (hasValidLogin == NO && /* I don't know to find a controller which requires login access*/) {
[LoginView showModal:viewController delegate:self];
return NO;
}
return YES;
}
pragma mark -
pragma mark LoginDelegate methods-
-(void)loginViewController:(LoginView *)loginViewController didLoginSuccess:NSString *)userName {
NSLog((#"%s [Line %d] "), __func__, __LINE__);
NSLog(#"userName = %#", userName);
NSUserDefaults *standardUserDefaults = [NSUserDefaults standardUserDefaults];
[standardUserDefaults setBool:YES forKey:#"hasValidLogin"];
[standardUserDefaults synchronize];
[self.tabBarController setSelected:/* How do i find the last touched tabbar*/];
}
Any good practice,recipes and code snippet you can direct me is appreciated.
I was reading matt's iOS Recipes. But I couldn't find anything related to this one.
Focus on UITabBarDelegate didSelectItem. Keep track of the previously selected tab item. If the user is not authenticated, pop up your authentication screen and select the previously selected tab item. Here is an example.
- (void)tabBar:(UITabBar *)tabBar didSelectItem:(UITabBarItem *)item {
switch ( item.tag ) {
case ME_TAB_BAR_TAG:
if ( [AppHelper checkAuthentication] ) {
[self showMe:YES];
} else if ( previousItem ) {
[tabBar setSelectedItem:previousItem];
return;
}
break;
case ITEMS_TAB_BAR_TAG:
[self showMe:NO];
break;
case CREATE_TAB_BAR_TAG:
if ( [AppHelper checkAuthentication] ) {
[self createNewItem];
} else if ( previousItem ) {
[tabBar setSelectedItem:previousItem];
return;
}
break;
}
previousItem = item;
}