locking tab bar items and unlocking via login page - objective-c

Hey guys having trouble with the UI tab controller,
So I have a login screen as my first controller I was wondering if there is a way to lock tab bar items and then unlocking them after login is successful?
At the moment I'm using NSDictionary to store my username and passwords and an UIAlertView for alerting whether the login is successful or not, but there really is no point of a login if tabs are accessible without the logging in.
below is my current code for logging in
- (IBAction)loginNow {
if ([[credentialsDictionary objectForKey:usernameField.text]isEqualToString:passwordField.text]) {
UIAlertView *alert = [[UIAlertView alloc] initWithTitle:#"Correct" message:#"You have successfully logged in." delegate:self cancelButtonTitle:#"Dismiss" otherButtonTitles:nil];
[alert show];
usernameField.hidden = YES;
passwordField.hidden = YES;
goButton.hidden = YES;
welcomeField.hidden = NO;
nameField.hidden = NO;
} else {
UIAlertView *alert = [[UIAlertView alloc] initWithTitle:#"You have entered an incorrect Username or Password" message:#"Please try again." delegate:self cancelButtonTitle:#"Dismiss" otherButtonTitles:nil];
[alert show];
}
}

You can use the tabBarController:shouldSelectViewController: method in UITabBarControllerDelegate protocol to return NO for tabs which should be inaccessible.
Set the relevant object (probably your main view controller) as the delegate of the tab bar, and implement the above method, returning NO when the user is not logged in and tries to access a tab which they should not have access to.

Related

Objective-c action before closing the window

How can i assign an action for close button in my window in objective-c?
For example, user clicks on close button and then program asks: "Do you really want to close the window?"
If you want to add message before closing view in Application you need to implement UIAlertView in it, don't forget to add < UIAlertViewDelegate> in .h file
code in action button pressed;
UIAlertView *alert = [[UIAlertView alloc] initWithTitle:#"Message"
message:#"Do you really want to close the window?"
delegate:self
cancelButtonTitle:#"OK"
otherButtonTitles:nil];
[alert show];
If you want to do something when the button is clicked, implement this delegate method:
- (void)alertView:(UIAlertView *)alertView didDismissWithButtonIndex:(NSInteger)buttonIndex {
// the user clicked OK
if (buttonIndex == 0) {
// Write Close Code here.....
}
}
Define an uialertview
UIAlertView *message;
message= [[UIAlertView alloc] initWithTitle:#"LOGOUT!" message:#"Are you sure to Logout of This app?" delegate:nil cancelButtonTitle:#"OK" otherButtonTitles:nil];
case :
{
[message show];
vc = [mainStoryboard instantiateViewControllerWithIdentifier:#"loginVC"];
[[SlideNavigationController sharedInstance] popAllAndSwitchToViewController:vc withCompletion:nil];
break;}
}
default:
break;
}

Successful login allows user to see information otherwise an Alert View is Displayed

I'm trying to implement code that only certain users can access. So when the username enters their name in the 'username' textfield it will return the values in the UITableView. When I sign in using username it won't display the array and displays the alert. What am I doing wrong? usernameText is the outlet into the storyboard.
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
// Return the number of rows in the section.
LoginViewController *userInfo = [[LoginViewController alloc] init];
if ([userInfo.usernameTxt.text isEqualToString:#"username"])
{
_loginSuccess = [flashesArray count];
}
else
{
UIAlertView *alert = [[UIAlertView alloc] initWithTitle:#"Access Denied" message:#"Please Sign Up For This Services" delegate:self cancelButtonTitle:#"Dismiss" otherButtonTitles:nil, nil];
[alert show];
}
return _loginSuccess;
}
There are several issues in your code:
You've created a new instance for 'LoginViewController'. That means 'userInfo.usernameTxt.text' is always empty. You have to pass the Loginname from your LoginViewController to the controller which handles the table.
You compare the loginname always with #"username". I think you want to compare with a real loginname

Objective C - SLComposeViewController delayed presention

I've been doing a program which takes care of posting a picture or words (or both) to both facebook and twitter. But I want to do them both at the same time, so I wrote my code like this:
//POST TO FACEBOOK
if ([SLComposeViewController isAvailableForServiceType:SLServiceTypeFacebook]) {
slcvc = [SLComposeViewController composeViewControllerForServiceType:SLServiceTypeFacebook];
[slcvc addImage:bim];
[slcvc setInitialText:tf.text];
[self presentViewController:slcvc animated:YES completion:NULL];
}
else {
UIAlertView *alert = [[UIAlertView alloc] initWithTitle:#"Facebook - not logged in!" message:#"You need to login (or sign up) to successfully post..." delegate:nil cancelButtonTitle:#"Too bad!" otherButtonTitles:nil];
[alert show];
}
//POST TO TWITTER
if ([SLComposeViewController isAvailableForServiceType:SLServiceTypeTwitter]) {
slcvc = [SLComposeViewController composeViewControllerForServiceType:SLServiceTypeTwitter];
[slcvc addImage:bim];
[slcvc setInitialText:tf.text];
[self presentViewController:slcvc animated:YES completion:NULL];
}
else {
UIAlertView *alert = [[UIAlertView alloc] initWithTitle:#"Twitter - not logged in!" message:#"You need to login (or sign up) to successfully post..." delegate:nil cancelButtonTitle:#"Too bad!" otherButtonTitles:nil];
[alert show];
}
This is all of course, in an IBAction function that is "file owned" already (slcvc is the SLComposeViewController, bim is an UIImage, and tf.text is the text of the UITextfield tf). And I have posted with this code before, only that it worked separately. If I try to use this to post a picture to Facebook and Twitter at the same time, I get this error:
Attempt to present <SLTwitterComposeViewController: 0xf6265e0> on <ViewController: 0x9476960> which is waiting for a delayed presention of <SLFacebookComposeViewController: 0x9432d70> to complete
(I'm still allowed to post to Facebook but not to Twitter)
I'm sure this happens because the SLComposeViewController registers itself as free to operate again once the first posting (the one for Facebook in this case) is done. So is there a way to have the second posting (the one for Twitter) to wait somehow for the user to send the first posting (to Facebook) and then to present the posting for Twitter? Thanks to anyone in advance for any help or suggestions!!
You will need to use the completion handler of the SLComposeViewController. This is called after user is done composing the post or cancelling it:
[slcvc setCompletionHandler:^(SLComposeViewControllerResult result) {
//POST TO TWITTER
if ([SLComposeViewController isAvailableForServiceType:SLServiceTypeTwitter]) {
slcvc2 = [SLComposeViewController composeViewControllerForServiceType:SLServiceTypeTwitter];
[slcvc2 addImage:bim];
[slcvc2 setInitialText:tf.text];
[self presentViewController:slcvc animated:YES completion:NULL];
}
else {
UIAlertView *alert = [[UIAlertView alloc] initWithTitle:#"Twitter - not logged in!" message:#"You need to login (or sign up) to successfully post..." delegate:nil cancelButtonTitle:#"Too bad!" otherButtonTitles:nil];
[alert show];
}
}
Just try following thing.
[[NSOperationQueue mainQueue] addOperationWithBlock:^{
[self presentViewController:fbViewController animated:YES completion:nil];
}];

How do I send the iOS user an instant local notification?

I simply want a notification to pop up when the user presses a button. No servers or timers needed. All the tutorials I can find seem to involve one of these two.
Maybe something like this:
-(IBAction) buttonPressed: (UIButton *)sender{
UIAlertView *alert = [[UIAlertView alloc] initWithTitle: [NSString stringWithFormat:#"You pressed button %#", sender.tag]
message: #"message"
delegate: self
cancelButtonTitle: #"Ok"
otherButtonTitles: nil]];
[alert show];
}
It would look something like this:
You can also customize the alert view a bit, so it could potentially have a textfield for text entry:
With UIAlertViews, you can also implement protocol methods like – alertView:clickedButtonAtIndex: and – alertView:willDismissWithButtonIndex:to perform different actions depending on which alert button they pressed.
Here's a good tutorial on UIAlertViews and implementing their protocol methods: http://mobile.tutsplus.com/tutorials/iphone/uialertview/.
If you don't want to use UIAlertViews and instead want a more customizable modal view, check out these two great libraries called UAModalPanel and MJPopupViewController. You can check out the links for images, demos, and more info on the two libraries, including links to their github pages where you can download them.
Hope this helps!
UIAlertView *alertView = [[UIAlertView alloc] initWithTitle:title message:message delegate:delegate cancelButtonTitle:cancelMessage];
[alertView show];
It is important to make sure the alert is shown on the main thread, where all the UI is handled. Otherwise you may get some weird errors and/or crashes. You can use GCD to dispatch to the main thread:
UIAlertView *alert = [[UIAlertView alloc] initWithTitle:#"Alert Test"
message:#"This is an alert test."
delegate:self
cancelButtonTitle:#"Cancel"
otherButtonTitles:#"OK",
nil];
dispatch_async(dispatch_get_main_queue(), ^{
[alert show];
});

UIAlertView delegate methods don't response on iOS 5.1

I created an instance of UIAlertView with two buttons and in the interface file of my class(.h) I set the delegate too but still cant get any response when clicking the buttons. Here is my code:
//myClass.h
#interface MainMenu : UIViewController<UIAlertViewDelegate>
-(IBAction)secondAct:(id)sender;
And the implementation
-(IBAction)secondAct:(id)sender
alert = [[UIAlertView alloc] initWithTitle:#"Dear User"
message:#"Your Request Will be Sent To Security"
delegate:nil
cancelButtonTitle:#"Cancel"
otherButtonTitles:#"OK", nil];
[alert show];
[alert autorelease];
}
and the delegate method:
-(void)alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex{
NSLog(#"UIAlertView delegate works");//this line too isnt displayed
NSString *title=[ alertView buttonTitleAtIndex:buttonIndex ];
if ([title isEqualToString:#"OK"]) {
NSLog(#"OK Pressed");
}//i want to create something like this
}
I did all of the code above but still can't take any action. Whichever of the buttons i click, it dissmisses the alert. What is wrong with this code can any one help?
ANSWER
Dear PeterG. commented to change delegete:nil with delegate:self and it works now.
Delegate should probably be set to self. If it generates an error post it here.
Also I do not think you should autorelease the alert ivar. Try to skip that line and see what happens?
Just give the delegate "self".
-(IBAction)secondAct:(id)sender
alert = [[UIAlertView alloc] initWithTitle:#"Dear User"
message:#"Your Request Will be Sent To Security"
delegate:self
cancelButtonTitle:#"Cancel"
otherButtonTitles:#"OK", nil];
[alert show];
}