alertView from appDelegate does not switch view uppon buttonIndex - ios

I am trying to create an alertView from the appDelegate but I cant get it to work. The alertView will act as a disclaimer when the app launches for the first time. I cant get it to work..the introViewcontroller does not appear. what am I doing wrong? ps. i use storyboards except for the intoViewController which is a nib file.my root view controller is a "fisrtViewController" here is my code: Thank you..
int a;
and in didFinishLaunchingWithOptions
if ( a == 0) {
UIAlertView *disclaimer = [[UIAlertView alloc] initWithTitle:#"Read Before use" message:#"By using this app you agree to its terms and conditions.\n\n\n\n\n\n\n\n\n\n\ntext heren\n\n\n\n\n\n\n\n\n\n\n\n\n" delegate:self cancelButtonTitle:#"No!" otherButtonTitles:#"Yes Let me In", nil];
[disclaimer show];
}
// Override point for customization after application launch.
return YES;
}
-(void) alertView:(UIAlertView *) alertView clickedButtonAtIndex:(NSInteger)buttonIndex{
if (buttonIndex == 1) {
// FirstViewController *firstView = [[FirstViewController alloc] init];
a+=1;
// [self.viewController presentModalViewController:firstView animated:YES];
}
else if (buttonIndex == 2) {
UIAlertView *alert = [[UIAlertView alloc] initWithTitle:#"Sorry!" message:#"You are not allowed to use this app due to the fact that you did not agree to the terms and Conditions. Please exit this app!" delegate:self cancelButtonTitle:#"OK" otherButtonTitles:nil, nil];
[alert show];
if (buttonIndex ==1) {
introViewController *intro = [[introViewController alloc] initWithNibName:#"introViewController" bundle:nil];
[self.viewController presentModalViewController:intro animated:YES];
introViewController *intro = [[introViewController alloc] initWithNibName:#"introViewController" bundle:nil];
[self.viewController presentModalViewController:intro animated:YES];
}
}

ahh... Now I Understand what you are trying to do.
You problem is that self.viewController is not set before you try to present a new modal ViewController. Also, I think your buttonIndex is of by one.
Try instead:
if (buttonIndex == 1)
{
_window = [[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]];
introViewController *intro = [[introViewController alloc] initWithNibName:#"introViewController" bundle:nil];
_window.rootViewController = _intro;
[_window makeKeyAndVisible];
}
Since your problem has nothing to to do with the UIAlertView, You might want to change the title and description of your question to make it more clear, for others struggling with a similar problem.
Happy coding! :)

The best way to handle this use a some NSString value write to NSUserDefaults file. when the app first time launch with the accepting the disclaimer (eg:#"accept") if user not accepting also update that NSString value accordingly.(#"notAccept").
In didFinishLaunchingWithOptions
if (![[[NSUserDefaults standardUserDefaults]valueForKey:#"key_disclaimer"] isEqualToString:#"accepted"]) {
UIAlertView *disclaimer = [[UIAlertView alloc] initWithTitle:#"Read Before use" message:#"By using this app you agree to its terms and conditions.\n\n\n\n\n\n\n\n\n\n\ntext heren\n\n\n\n\n\n\n\n\n\n\n\n\n" delegate:self cancelButtonTitle:#"No!" otherButtonTitles:#"Yes Let me In", nil];
[disclaimer show];
[disclaimer release];
// make sure to release it.
}
//if user accept the disclaimer.
[[NSUserDefaults standardUserDefaults]setValue:#"accepted" forKey:#"key_disclaimer"];
//if user not accept the disclaimer,
[[NSUserDefaults standardUserDefaults]setValue:#"notAccepted" forKey:#"key_disclaimer"];
update "key_disclaimer" accordingly. thats the best way. in your example the value of a using in the temporally. if you close and restart the app it will ask again the disclaimer.
in your code
int a;
replace it by using
int a = 0;
it will be ok.
thanx.

If your code is exactly like you wrote above, a won't be 0.
If you don't explicitly set it to 0 at startup, it might have any value.
If you want to check if the app was opened the first time after installing, use NSUserDefaults
e.g. (in didFinishWithOptions):
int startCount=[[NSUserDefaults standardUserDefaults] intForKey:#"startCount"];
if (startCount==0]){
//Is the first start, show your agreement
//Then increase startCount so that this will not be called at next start
[[NSUserDefaults standardUserDefaults] setInteger:startCount++ forKey:#"startCount"];
}
You could also store your version number in the NSUserDefaults. This way, you could check if the user first starts a new version.

Related

APNS to open a certain part of an application

I've just implemented a commenting feature in my app. Ideally when someone leaves a comment, I'd like all notified people be able to swipe the push notification and open the app on that post.
I assume you want to open the concerned page directly. There are many ways to go about this, and it depends on how your app is laid out.
If you want to open an inner page upon app launch, you can programmatically trigger the segues that the user would otherwise need to make manually. (this ensures the back/home buttons work as opposed to loading the desired page directly).
Here's an excerpt from one of my own code, your use case may not be the same, but this is all i can do unless you give us more details.
- (BOOL) navigateToRespectiveSectionforPushNot:(NSDictionary*)pushNot
{
id rootVC = self.window.rootViewController;
NSLog(#"ROOT CLASS : %#", [rootVC class]);
if ([rootVC isKindOfClass:[SWRevealViewController class]])
{
NSLog(#"Root Class looking good... mission Navigate!!");
SWRevealViewController *homeVC = (SWRevealViewController*) rootVC;
NSString *category = [[pushNot objectForKey:pushPayloadKeyaps] objectForKey:pushPayloadKeyCategory];
NSString *subCat = [[pushNot objectForKey:pushPayloadKeyaps] objectForKey:pushPayloadKeySubCategory];
NSLog(#"category : %# , subcat : %#",category,subCat);
//The code for the page to which i'm supposed to navigate to is contained in the push notification payload
if ([category isEqualToString:pushCategoryItemChat])
{
[homeVC.rearViewController performSegueWithIdentifier:#"chatPush" sender:nil];
UINavigationController *nc = (UINavigationController*)homeVC.frontViewController;
NSLog(#"FrontView Class : %#",[nc.viewControllers[0] class]);
UITableViewController *tvc = (UITableViewController*)nc.viewControllers[0];
NSDictionary *send = #{chatPushTargetUserId:subCat,chatPushTargetUserName:#"",chatPushTargetUserImage:#""};
[tvc performSegueWithIdentifier:#"seguePushDemoVC" sender:send];
return YES;
}
//communityPush historyPush
else if ([category isEqualToString:pushCategoryItemCommunity])
{
if ([subCat isEqualToString:pushSubCatItemNewRequest])
{
[homeVC.rearViewController performSegueWithIdentifier:#"communityPush" sender:nil];
return YES;
}
else if ([subCat isEqualToString:pushSubCatItemAccepted])
{
[homeVC.rearViewController performSegueWithIdentifier:#"communityPush" sender:nil];
return YES;
}
}
else if ([category isEqualToString:pushCategoryItemHistory])
{
[homeVC.rearViewController performSegueWithIdentifier:#"historyPush" sender:nil];
return YES;
}
}
else
{
UIAlertView *whoa = [[UIAlertView alloc] initWithTitle:#"WHOA!!" message:#" That wasn't supposed to happen. You are not even logged in. Call 911..." delegate:nil cancelButtonTitle:#"mmKay.." otherButtonTitles:nil, nil];
[whoa show];
}
return NO;
}
I hope the code is self explanatory. cheers

add confirmation for deleting using UIActionSheet

When you long press on an item in my app a UIMenuController pops up and gives me a few options. One of which is to delete.
Here is an example of what that looks like:
- (void)messageCellDidLongPress:(FCIMessageCell *)cell
{
self.cellShowingMenuController = cell;
[self becomeFirstResponder];
self.messageMenuController = [UIMenuController sharedMenuController];
self.messageMenuController.anchorView = cell.bodyLabel;
self.messageMenuController.presentInView = cell;
[self.messageMenuController updatePosition];
UIMenuItem *editMenuItem = [[UIMenuItem alloc] initWithTitle:#"Edit" action:#selector(edit:)];
UIMenuItem *unsendMenuItem = [[UIMenuItem alloc] initWithTitle:#"Unsend" action:#selector(unsend:)];
UIMenuItem *infoMenuItem = [[UIMenuItem alloc] initWithTitle:#"Details" action:#selector(info:)];
self.messageMenuController.menuItems = #[unsendMenuItem, editMenuItem, infoMenuItem];
[self.messageMenuController setMenuVisible:YES animated:YES];
//Delete button doesn't appear because its set up to appear on conditions
}
What I want to do: Once someone presses the delete button have a "CONFIRM" or "CANCEL" option appear, probably using a UIActionSheet. I'm not too sure how to do that. Where would I put the code to get it to appear. I'm not sure if I need to create an entire other view, or what?
In .h
#interface MyViewController : UIViewController <UIActionSheetDelegate>
In .m
-(IBAction) edit:(id)sender
{
self.editSheet = [[UIActionSheet alloc] initWithTitle:nil
delegate:self
cancelButtonTitle:#"cancel"
destructiveButtonTitle:#"delete"
otherButtonTitles:nil];
[self.editSheet showFromBarButtonItem:sender animated:YES];
}
and later on, in .m
- (void)actionSheet:(UIActionSheet *)actionSheet
clickedButtonAtIndex:(NSInteger)buttonIndex
{
if (self.editSheet == actionSheet) {
if((actionSheet.cancelButtonIndex) == buttonIndex) {
// Cancel, do nothing
} // etc.
}
There is a bit more to it, like not creating a sheet if it already exists, possibly handling iPhone & iPad differently, etc.

UIAlertView button responds to "Return" key

I have a UIAlertView with UIAlertViewStyleSecureTextInput. When the user taps "Login" and the password is correct, it will push the next ViewController. If the password is incorrect, it displays another UIAlertView prompting a single "Dismiss" button. What I am trying to do is when the user taps the return key, the "Login" button will be triggered. As it sits now, when the return key is pressed the alert just dismisses, regardless if the password is correct or not. Maybe there is a more logical solution than what I have attempted? Sorry if the title is a little confusing, I don't know how else to explain it. Any help is greatly appreciated. Thanks.
I have declared the first alert in my .h file, and conformed to the UIAlertViewDelegate and UITextFieldDelegate:
#interface EndViewController : UIViewController <UIAlertViewDelegate, UITextFieldDelegate>
#property (retain, strong) UIAlertView *loginRequiredMsg;
Method for the "Login" alert:
- (IBAction)resultsBtnPressed:(UIButton *)sender {
self.loginRequiredMsg = [[UIAlertView alloc]initWithTitle:#"Login Required"
message:#"Please enter the admin password"
delegate:self
cancelButtonTitle:#"Cancel"
otherButtonTitles:#"Login", nil];
self.loginRequiredMsg.alertViewStyle = UIAlertViewStyleSecureTextInput;
[self.loginRequiredMsg textFieldAtIndex:0].delegate = self;
[self.loginRequiredMsg show];
}
Method for dismissing the keyboard when return key is tapped (I think I need to somehow call the next alert here if the password is incorrect, as currently it just dismisses the alert):
- (BOOL)textFieldShouldReturn:(UITextField *)textField {
// Dismiss keyboard when return key is pressed
[self.loginRequiredMsg dismissWithClickedButtonIndex:self.loginRequiredMsg.firstOtherButtonIndex animated:YES];
return YES;
}
And finally the method containing the outcome of the password entered:
- (void)alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex
{
NSString *title = [alertView buttonTitleAtIndex:buttonIndex];
if([title isEqualToString:#"Login"]) {
UITextField *password = [alertView textFieldAtIndex:0];
// Basic login authentication
if ([password.text isEqualToString:#"admin"]) {
// Allocate & initialise ViewController
ResultsViewController *Results = [[ResultsViewController alloc]initWithNibName:#"ResultsViewController" bundle:nil];
// Push next ViewController
[self.navigationController pushViewController:Results animated:YES];
NSLog(#"Show results");
} else {
UIAlertView *errorMsg = [[UIAlertView alloc]initWithTitle:#"Error"
message:#"Admin password is incorrect. Please try again."
delegate:self
cancelButtonTitle:#"Dismiss"
otherButtonTitles:nil];
[errorMsg show];
}
}
}
As discussed in the comments, you need to implement alertView:didDismissWithButtonIndex: which tells you when the alert view has been dismissed. When you tap the return key, it calls that method with the index of the OK button.

iOS and xcode: how to create a modal segue to a special view only the first time the app is used

I want to make a "terms and conditions" screen that pops up the very first time that app is opened. On this view I would add a button that says "agree," and upon clicking it the code would execute:
[self dismissViewControllerAnimated:YES completion:nil];
...and would go to the first view of the app.
I am currently using a Tab Bar Controller that has 4 ViewControllers. So basically, I just need to have some method in viewWillAppear on my first ViewController that checks for an NSUserDefault key:value. The first time the app opens, it will be zero. After they click agree, I'll set it to 1 and the bit of code would never execute again.
Can you please offer some code to accomplish the task of routing the view from the firstViewController's view to this alternate view controller upon loading the app?
Thanks!
In the viewWillAppear method in your FirstViewController, check NSUserDefaults then present your TermsViewController. After user click agree in TermsViewController, set NSUserDefaults then call
[self.presentingViewController dismissModalViewControllerAnimated:YES]
The use of the popover window can get complicated. Try something like the following if you have little experience with Objective-C.
- (void)viewDidLoad {
if ([termsvalue == 0]) {
NSString *msg = [NSString stringWithFormat:#"Do you agree with the terms of use?"];
UIAlertView *alert = [[UIAlertView alloc] initWithTitle:#"- Confirmation -"
message:msg
delegate:self
cancelButtonTitle:#"Disagree"
otherButtonTitles:#"I agree", nil];
[alert setTag:100];
[alert show];
}
}
- (BOOL)alertViewShouldEnableFirstOtherButton:(UIAlertView *)alertView { // Validation
if ([alertView tag] == 100) {
return YES;
}
}
- (void)alertView:(UIAlertView *)alertView didDismissWithButtonIndex:(NSInteger)buttonIndex { // Go
if ([alertView tag] == 100) {
if (buttonIndex == 1) {
// The user has agreed
}
}
}

how can I hide an empty tableview

I have a problem and I hope someone here can help me out! Thanks in advance.
I have a "Scope"button(see screen shot!) which is linked to a segue and the segue goes to a tableview.
What I want to do is in the tableview controller that I decide if I should display something in the table or I should throw out an alertview.
What I did was:
if ([_countryScopes count] == 0) {
[self.view removeFromSuperview];
//no scope for this country
NSMutableString *message = [[NSMutableString alloc]initWithString:#"No scope information."];
UIAlertView *av = [[UIAlertView alloc] initWithTitle:countryName message:message delegate:self cancelButtonTitle:#"Ok" otherButtonTitles:nil, nil];
//av.tag = MAIN_ALERT;
[av show];
}
But somehow the empty table is still showing up.
Do you know, how I can get rid of this?
Thanks a lot!
Regards, Yashu
just show alert view after 0.1 seconds, i hope this will do the trick:
[av performSelector:#selector(show) withObject:nil afterDelay:0.1];

Resources