how to auto dismiss UIAlertView when app becomes active - xcode

OK, I have an app that runs a timer. If the user is watching the app's timer count down (e.g. the app is awake and active in the foreground) I want an alert to be shown to the user. I've added this code to my timer when it reaches 0:
UIAlertView *alert = [[UIAlertView alloc] initWithTitle:#"Reminder" message:#"It's time!" delegate:nil cancelButtonTitle:#"OK" otherButtonTitles:nil, nil];
[alert show];
The problem I'm having is that if I put the phone to sleep or make the app inactive in some other fashion I have a local notification setup to handle this alerting so when the user goes back to the app I don't want them to see the alert mentioned above. It's an unnecessary "click" they have to make.
Is there a way to auto dismiss this alert when the app either goes into the background or enters the foreground if it's been triggered?

You have to use NSNotificationcenter at the UIAlertview definition.
UIAlertView *alert = [[UIAlertView alloc] initWithTitle:title message:message delegate:nil cancelButtonTitle:nil otherButtonTitles:#"OK",nil];
[alert show];
[[NSNotificationCenter defaultCenter] addObserverForName:UIApplicationWillResignActiveNotification object:nil queue:[NSOperationQueue mainQueue] usingBlock:^(NSNotification* notification){
[alert dismissWithClickedButtonIndex:0 animated:NO];
}];

There is no auto dismiss.
You have to communicate that your app become active (from UIApplicationDelegate) to your UIAlertView.
To do so, many techniques exists, you could keep reference of that alert in AppDelegate to dismiss it, or use some NSNotification posted from delegate and observed in your ViewController or anywhere you are showing this alert.

Alternatively you could use... https://github.com/sdarlington/WSLViewAutoDismiss

Related

Why are all my UIAlertView labels/text bold by default in iOS 8

I searched for this and the only solutions seem to be derive from the UIAlertViewDelegate. I don't want to do that just to eliminate bold text.
The code that I use to pop my alert view is the following:
NSString* errPrompt = #"some text here, anything that will not show bold :)";
UIAlertView* alert = [[UIAlertView alloc] initWithTitle:title
message:errPrompt
delegate:nil
cancelButtonTitle:[self getUiText:"OK"]
otherButtonTitles:nil];
[alert show];
This is a bug in iOS and affects all alerts which do not have a title set.
Interestingly most standard iOS alerts (like in App Store) are not affected.
This works for me on iOS 8. Just empty string in the title without space inside #"".
[[[UIAlertView alloc] initWithTitle:#""
message:#"My message"
delegate:nil
cancelButtonTitle:#"OK"
otherButtonTitles:nil] show];
This happens when you do not set Alert title. (Weird, I know)
Set Alert's title as. empty if you. do not want to add any title
UIAlertController(title: "", message: "The alert message", preferredStyle: .alert)

UIAlertView with UIImageView in accessoryView

I am giving an UIImageView in the UIAlertView's accessoryView by the following code:
UIAlertView *alertView = [[UIAlertView alloc]init];
[alertView addButtonWithTitle:#"Cancel"];
[alertView addButtonWithTitle:#"OK"];
UIImageView *someImageView = /*initialization code */;
[someImageView setFrame:CGRectMake(0,0,40 ,20)];
[alertView someImageView forKey:#"accessoryView"];
[alertView show];
The above code works fine with iOS7 but since the update to iOS8 the alertView looks elongated and disoriented. I have tried changing the constraints and the aspect ratio of the image that is set in the UIImageView.
Can someone help me with this?
This is not a supported use of UIAlertView:
The UIAlertView class is intended to be used as-is and does not support subclassing. The view hierarchy for this class is private and must not be modified.
Additionally, UIAlertView is deprecated in iOS 8.
You should build your own view to present, or use one of the many open-sourced options on GitHub/CocoaControls, like SDCAlertView or ios-custom-alertview.

Meraki guided access not working on iPAD

I am trying for for Single App mode for one of my App using "Meraki" MDM, registered the device on the MDM site and created the profile.
I am using below code as suggested in this site, the code works fine on simulator but not on iPad."UIAccessibilityRequestGuidedAccessSession" is not fired. Can someone help me what I am missing here.
NSLog(#"requesting guided access");
UIAccessibilityRequestGuidedAccessSession(YES, ^(BOOL didSucceed) {
if (didSucceed) {
NSLog(#"entered guided access");
self.inGuidedSessionMode = YES;
[[[UIAlertView alloc] initWithTitle:#"entered single access mode" message:nil delegate:nil cancelButtonTitle:#"OK" otherButtonTitles:nil] show];
}
else {
NSLog(#"failed to enter guided access");
[[[UIAlertView alloc] initWithTitle:#"Unable to enter single access mode" message:nil delegate:nil cancelButtonTitle:#"OK" otherButtonTitles:nil] show];
}
});
This will only work if the device is supervised, which is done through Apple Configurator or the Apple Device Enrollment Program.

UIAlertview delegate will crash when setting delegate to self

I have this simple piece of code:
UIAlertView *alert = [[UIAlertView alloc] initWithTitle:#"Contact" message:#"This contact does not exist yet" delegate:self cancelButtonTitle:#"Ok" otherButtonTitles:#"Not now", nil];
[alert show];
If I set delegate to 'nil', everything is fine. However, if I set delegate to 'self' and add either a clickedButtonAtIndex or didDismissWithButtonIndex delegate, the application crashes with EXC_BAD_ACCESS
I think you did not set the alertView delegate method.
First set the alertView delegate protocol in .h file.
#interface MainViewController : UIViewController<UIAlertViewDelegate>
Then implement this method, it will work fine
-(void)alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex
{
switch (buttonIndex)
{
case 0:
break;
case 1:
break;
default:
break;
}
}
your problem is that your object (self) doesn't exists anymore! but the alertview try to access it, so your get EXC_BAD_ACCESS. Check if your delegate object (self) is alive!
The problem was due to my flow. I have a class calling a URL. the Viewcontroller did in fact finish long before the response came form the server. I therefore had to implement an NSRunLoop in the caller in order to wait for the server communication to finish. Based on some ExitCode form the called routine, I could then only display an alert and get the delegate to handle the pressed button. Thanks anyway to Chakalaka for putting me on the track.

Removeallobjects in array from another view? Xcode iOS

Please forgive me is this is too vague. I have an app that has populated arrays based on where the user browses to. One is just all sites visited and is accessible in another viewcontroller and another is all text input in to the textview which is displayed while the user types. I don't know if any of that is important but my question is, I would like to have a settings page that has the option to clear that data. I can do it from the header file in that view its self but not sure how to send the removeallobjects command to other views or arrays.
In each view I have a button to call this:
-(IBAction)clearPreText {
UIAlertView *alert = [[UIAlertView alloc]
initWithTitle:#"clear all predictive typing?"
message:#"press ok to clear"
delegate: self
cancelButtonTitle:#"cancel"
otherButtonTitles:#"ok", nil];
[alert show];
}
-(void)alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex {
if (buttonIndex == 1) {
[pastUrls removeAllObjects];
[[NSUserDefaults standardUserDefaults] setObject:autocompleteUrls forKey:#"PastUrls"];
[self.autocompleteTableView reloadData];
}}
I would like to place those buttons in a new view, a settings page.
Thanks for anything.
Both controllers will have access to the app delegate and the app delegate will have access to each of your controllers. When I need to do something like this, I usually do it through the app delegate.

Resources