iOS 8.3: Alert View Cancel Button not responding in AppDelegate - objective-c

I am working on my project in iOS 8.3 in Xcode 6.3.2, I have placed an AlertView in AppDelegate class when my web service returns error, but when I am trying to click cancel button on Simulator, it is not responding. Does anyone facing same error?
Below is my code:
UIAlertView *alertFailure = [[UIAlertView alloc] initWithTitle:#"Error Occured" message:#"Something went wrong" delegate:self cancelButtonTitle:#"Ok" otherButtonTitles:nil];
[alertFailure show];
Edit 1: Added Delegate Method on request
- (void)alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex
{
// the user clicked OK
if (buttonIndex == 0) {
// do something here...
alertView.hidden = YES;
}
}
Edit 2: It is not even sensing the delegate
AppDelegate.h
#interface AppDelegate : UIResponder <UIApplicationDelegate, CLLocationManagerDelegate, UIAlertViewDelegate>

You have to implement the following delegate method in your application.
- (void)alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex;
Here whenever you will click on the Cancel Button then it will work without any problem. As seems that you have already set the delegate for UIAlertView.
Please let me know if you still face any issue.

If you didn't already, you need to add that the AppDelegate is conforming to UIAlertViewDelegate like this:
In AppDelegate.h:
#interface AppDelegate : UIResponder <UIApplicationDelegate, UIAlertViewDelegate>
Edit: I just created an empty project and pasted your code in applicationDidBecomeActive:, and it worked. No delegate methods, no nothing. Maybe remove the whole alertView:clickedButtonAtIndex: method?
Edit 2: If you're only targeting iOS 8+, then you can (and should, since UIAlertView is deprecated in iOS 8) use the new UIAlertController:
UIAlertController* alert = [UIAlertController alertControllerWithTitle:#"Error Occurred"
message:#"Something went wrong."
preferredStyle:UIAlertControllerStyleAlert];
UIAlertAction* defaultAction = [UIAlertAction actionWithTitle:#"OK" style:UIAlertActionStyleDefault
handler:^(UIAlertAction * action) {}];
[alert addAction:defaultAction];
[self.window.rootViewController presentViewController:alert animated:YES completion:nil];

If you're targeting iOS 8.3 you can use UIAlertController instead of UIAlertView, you don't need to implement any protocols or set a delegate, just use an action:
UIAlertController *alert = [UIAlertController alertControllerWithTitle:#"Error Occured"
message:#"Something went wrong"
preferredStyle:UIAlertControllerStyleAlert];
UIAlertAction *cancel = [UIAlertAction actionWithTitle:#"OK"
style:UIAlertActionStyleCancel
handler:NULL];
[alert addAction:cancel];
[self presentViewController:alert
animated:YES
completion:nil];

Related

Show keyboard when Textfield inside Alert in WkWebView is tapped

There is a game that i load in the WKWebView and this Alert and textField appear after the game is finished. I want to display keyboard when user taps on the textField. But the problem is there is no code to display this alert and textfield, otherwise i would have managed the textField to becomeFirstResponder().
Desperately need some help. I would really appreciate it.
Okay so i have found the solution. I had a textField in custom Alert/Popup in WkwebView and i wanted the textField to becomeFirstResponder() so i could take user input.
// So i Implemented WKUIDelegate..
#import <WebKit/WebKit.h>
#interface MyController : UIViewController<WKUIDelegate>
Assigned the Delegate..
- (void)viewDidLoad {
[super viewDidLoad];
_webkitView.UIDelegate = self;
}
//implemented delegate function.
- (void)webView:(WKWebView *)webView runJavaScriptTextInputPanelWithPrompt:(NSString *)prompt defaultText:(NSString *)defaultText initiatedByFrame:(WKFrameInfo *)frame completionHandler:(void (^)(NSString *result))completionHandler
{
UIAlertController *alert = [UIAlertController alertControllerWithTitle:#"BlockChaid Id, Email or Username"
message:prompt
preferredStyle:UIAlertControllerStyleAlert];
[alert addTextFieldWithConfigurationHandler:^(UITextField *textField) {
textField.placeholder = prompt;
textField.secureTextEntry = NO;
textField.text = defaultText;
}];
[alert addAction:[UIAlertAction actionWithTitle:NSLocalizedString(#"Cancel", nil) style:UIAlertActionStyleCancel handler:^(UIAlertAction *action) {
completionHandler(nil);
}]];
[alert addAction:[UIAlertAction actionWithTitle:NSLocalizedString(#"OK", nil) style:UIAlertActionStyleDefault handler:^(UIAlertAction *action) {
completionHandler([alert.textFields.firstObject text]);
}]];
[self presentViewController:alert animated:YES completion:nil];
}
After successful build, this was the output.
I tapped the textField, and javaScript Alert appeared.
This right here is what i wanted to achieve.
Maybe i was not good enough to explain my question, but this is what i was asking for. Thank you :)

in iOS 13 [UIApplication sharedApplication] delegate].window does not work using objective-c

I need to display alert from a class which is non-UIView using objective-c.
Below is the code:
UIAlertController *alertVC = [UIAlertController alertControllerWithTitle:#“Sample” message message:#“hello message” preferredStyle:UIAlertControllerStyleAlert];
[alertVC addAction:[UIAlertAction actionWithTitle:#"Okay" style:UIAlertActionStyleCancel handler:^(UIAlertAction * action) {}]];
[[[UIApplication sharedApplication] delegate].window.rootViewController presentViewController:alertVC animated:YES completion:nil];
this above does not work anymore and I could not any other alternative for same code in objective-c.
I came across this link How to resolve: 'keyWindow' was deprecated in iOS 13.0 but solution is in Swift,not in objective-c .
Thanks
The problem is that in iOS 13, this expression...
[[UIApplication sharedApplication] delegate].window.rootViewController
...is nil. That's because the window property belongs to the scene delegate, not the app delegate. You would do better to access the window by way of the UIApplication itself. For example, look through the application's windows and find the one that is key.
You are trying to present the viewcontroller in key window which will create exception. Try this code
UIAlertController *alert = [UIAlertController alertControllerWithTitle:#"Tap!" message:message preferredStyle:UIAlertControllerStyleAlert];
UIAlertAction *action = [UIAlertAction actionWithTitle:#"OK" style:UIAlertActionStyleDefault handler:nil];
[alert addAction:action];
[self presentViewController:alert animated:YES completion:nil];
I think in your case most simple would be to use the following as replacement of your last code row
[UIApplication.sharedApplication.windows.lastObject.rootViewController
presentViewController:alertVC animated:YES completion:nil];

UIAlertController automatically dismisses in iOS 10

I have a UIAlertController in my AppDelegate and present it with my rootViewController when I receive a notification. But when the alert view pops up, it quickly dismisses itself instead of waiting for a tap event on the "Ok" button... Here's my code in AppDelegate.m. Any idea? Thanks!
- (void)application:(UIApplication *)application didReceiveLocalNotification:(UILocalNotification *)notification {
[self showNotification:"Success message here"];
}
- (void)showNotification:(NSString *)text {
UIAlertController* avc = [UIAlertController alertControllerWithTitle:#"Success" message:text
preferredStyle:UIAlertControllerStyleAlert];
UIAlertAction *ok = [UIAlertAction actionWithTitle:#"Ok" style:UIAlertActionStyleCancel handler:nil];
[avc addAction:ok];
[self.window.rootViewController presentViewController:avc animated:true completion:nil];
}
EDIT: I have another view controller; call it X. X's viewDidLoad function has a timer counting down before X dismisses itself and presents another view controller. I noticed an interesting timing issue here: when the notification comes slowly enough so that the UIAlertController appears after X's timer is up and another view controller has been presented, the alertController doesn't dismiss itself. If the opposite happens - the notification comes quickly enough making the UIAlertController appears before X dismisses itself, then the whole AlertController will be flushed when the timer is up (which makes sense, I guess...). How can I prevent this from happening?
You can use perform selector method, like this:
- (void)showNotification:(NSString *)text {
UIAlertController* avc = [UIAlertController alertControllerWithTitle:#"Success" message:text
preferredStyle:UIAlertControllerStyleAlert];
UIAlertAction *ok = [UIAlertAction actionWithTitle:#"Ok" style:UIAlertActionStyleCancel handler:nil];
[avc addAction:ok];
[self performSelector:#selector(dissmissAlert:) withObject:avc afterDelay:3.0];
[self.window.rootViewController presentViewController:avc animated:true completion:nil];
}
-(void)dissmissAlert:(UIAlertController *) alert{
[alert dismissViewControllerAnimated:true completion:nil];
}

UIAlertView and UIActionSheet not displaying properly in iOS 8

UIAlertView and UIActionSheet not displaying properly in iOS 8, its hiding the view controller, also In alert view title is not displaying. I am running the app from Xcode 5.1.1.
Did you added any category for ViewController in your application. like below shown
#implementation UIViewController (CustomeAction)
- (void)setTitle:(NSString *)title{
// YOU CODE///
}
#end
This may the issue. I have solved by removing this category - (void)setTitle:(NSString *)title.
Note: From iOS 8, UIAlertViewController is inherited from UIViewController. If you use category method, it will be effect on UIAlertView & UIActionSheet titles.
You can refer apple docs at UIAlertController Class Reference
It should be noted, that UIAlertView and UIActionSheet is deprecated in iOS 8. You can check out this question or the apple link. May be it will help you.
You can use following code using UIAlerController for XCode 6 and IOS 8
UIAlertController * alert= [UIAlertController
alertControllerWithTitle:#"Title"
message:#"Your Message"
preferredStyle:UIAlertControllerStyleAlert];
UIAlertAction* ok = [UIAlertAction
actionWithTitle:#"OK"
style:UIAlertActionStyleDefault
handler:^(UIAlertAction * action)
{
[alert dismissViewControllerAnimated:YES completion:nil];
}];
UIAlertAction* cancel = [UIAlertAction
actionWithTitle:#"Cancel"
style:UIAlertActionStyleDefault
handler:^(UIAlertAction * action)
{
[alert dismissViewControllerAnimated:YES completion:nil];
}];
[alert addAction:ok];
[alert addAction:cancel];
[self presentViewController:alert animated:YES completion:nil];
For ActionSheet
UIAlertController * alert= [UIAlertController
alertControllerWithTitle:#"Title"
message:#"Your Message"
preferredStyle:UIAlertControllerStyleActionSheet];

popViewController doesn't work with UIAlertView

I am having problem with AlertView. I am trying to use the UIAlertView and after click ok it will return back to the previous screen but it do not seems to work any advice ?
if (xGPSCoordinate==0 && yGPSCoordinate == 0) {
UIAlertView *alert = [[UIAlertView alloc] initWithTitle:#"Title"
message:#"Failed to load the to get your current location"
delegate:self
cancelButtonTitle:#"Ok"
otherButtonTitles:nil, nil];
[alert show];
[alert release];
return;
[self.navigationController popViewControllerAnimated:YES];
}
or
if (xGPSCoordinate==0 && yGPSCoordinate == 0) {
UIAlertView *alert = [[UIAlertView alloc] initWithTitle:#"iPoly"
message:#"Failed to load the to get your current location"
delegate:self
cancelButtonTitle:#"Ok"
otherButtonTitles:nil, nil];
[alert show];
[alert release];
[self.navigationController popViewControllerAnimated:YES];
return;
}
both doesn't work
For this purpose you've to use UIAlertView's delegate method.
- (void)alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex;
First use this in your #interface <UIAlertViewDelegate>
Then set the delegate, self.yourAlertView.delegate=self;
- (void)alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex
{
if(buttonIndex==0)//first button which should be the OK button
{
[self.navigationController popViewControllerAnimated:YES];
}
}
use the delegate method of UIAlertView, see the answer given by iNoob. It does not make a sense if you write anything after the "return;" statement as the code below "return;" statement will never get executed.
refer apple developer link for more details on UIAlertView delegate http://developer.apple.com/library/ios/#DOCUMENTATION/UIKit/Reference/UIAlertViewDelegate_Protocol/UIAlertViewDelegate/UIAlertViewDelegate.html
or a simple tutorial on alert view
http://mobile.tutsplus.com/tutorials/iphone/uialertview/
You just need to implement UIAlerView Delegate Methods.
- (void)alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex{}
write your code for Pop to previous Controller here.You can the clicked button index and on the basis of that you can use.Don't for Conform UIAlertViewDelegate to Interface.