Create Local Notification on iphone - ios

I am trying to create a local Notification button like "Are you sure you want to delete yes or no? How would I go about doing this

try this :
UIAlertView *alert = [[UIAlertView alloc] initWithTitle:#"Alert!" message:#"Are you sure you want to delete?" delegate:self cancelButtonTitle:nil otherButtonTitles:#"Yes", #"No", nil];
alert.tag=1;
[alert show];
- (void)alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex
{
if(alertView.tag==1)
{
if(buttonIndex==0)
{
//Do when click "Yes" button
}
else if(buttonIndex==1)
{
//Do when click "No" button
}
}
}
may be it will help you.

Based on what you are trying to accomplish, I do not think you want to use a UILocalNotification, looks like what you want is a UIAlertView
UIAlertView *alert = [[UIAlertView alloc] initWithTitle:#"My title" message:#"Are you sure you want to delete?" delegate:self cancelButtonTitle:nil otherButtonTitles:#"Yes", #"No", nil];
[alert show];
Hope that helps

You can use a UIAlertView for what you want to do, just like Oscar Gomez says.
To add actions to the buttons first implement the UIAlertViewDelegate and use the method
- (void)alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex
You can check the buttonIndex and perform the appropriate action, ( index 0 for button
"Yes" and 1 for button "No").

This code block in AppDelegate file :
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{
[launchOptions valueForKey:UIApplicationLaunchOptionsLocalNotificationKey];
// Override point for customization after application launch.
return YES;
}
// This code block is invoked when application is in foreground (active-mode)
-(void)application:(UIApplication *)application didReceiveLocalNotification:(UILocalNotification *)notification {
**UIAlertView *notificationAlert = [[UIAlertView alloc] initWithTitle:#“Alert” message:#“Are you sure you want to delete?” delegate:self cancelButtonTitle:nil otherButtonTitles:#“Yes”,#“No”, nil];
notificationAlert.tag = 101;
[notificationAlert show];**
// NSLog(#"didReceiveLocalNotification");
}
**- (void)alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex {
if(alertView.tag==1) {
if(buttonIndex==0)
{
//Do when click "Yes" button
}
else if(buttonIndex==1)
{
//Do when click "No" button
}
}
}**
This code block in .m file of any ViewController :
-(IBAction)startLocalNotification { // Bind this method to UIButton action
NSLog(#"startLocalNotification");
UILocalNotification *notification = [[UILocalNotification alloc] init];
notification.fireDate = [NSDate dateWithTimeIntervalSinceNow:7];
notification.alertBody = #"This is local notification!";
notification.timeZone = [NSTimeZone defaultTimeZone];
notification.soundName = UILocalNotificationDefaultSoundName;
notification.applicationIconBadgeNumber = 10;
[[UIApplication sharedApplication] scheduleLocalNotification:notification];
}
The above code display an AlertView after time interval of 7 seconds when pressed on button that binds “startLocalNotification”.
If application is in background then it displays BadgeNumber as 10 and with default notification sound.

Related

UIAlertView button action not working

I have one alert view and when I click on yes button it is supposed to produce another alert view and a toast message,but it is not happening. I couldn't figure it out. Here is my code:
-(void)myMethod {
UIAlertView *saveAlert = [[UIAlertView alloc] initWithTitle:#"First Message"
message:#"My First message"
delegate:nil
cancelButtonTitle:#"No"
otherButtonTitles:#"Yes", nil];
saveAlert.tag=0;
[saveAlert performSelectorOnMainThread:#selector(show) withObject:nil waitUntilDone:NO];
}
This is the method I am using to provide the functionality for different alert views.
- (void)alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex
{
if(alertView.tag==0) {
if (buttonIndex == 0)
{
//Code for Cancel button
}
if (buttonIndex == 1)
{
//code for yes button
MBProgressHUD *hud = [MBProgressHUD showHUDAddedTo:self.navigationController.view animated:YES];
hud.mode = MBProgressHUDModeText;
hud.labelText = #"Successfully displayed First Message";
hud.margin = 10.f;
hud.yOffset = 150.f;
hud.removeFromSuperViewOnHide = YES;
[hud hide:YES afterDelay:3];
UIAlertView *alert = [[UIAlertView alloc] initWithTitle:#"Second Message"
message:#"My second message"
delegate:nil
cancelButtonTitle:#"No"
otherButtonTitles:#"Yes",nil];
alert.tag=1;
[alert performSelectorOnMainThread:#selector(show) withObject:nil waitUntilDone:YES];
}
}
if (alertView.tag==1) {
if (buttonIndex == 0)
{
//Code for Cancel button
}
if (buttonIndex == 1)
{
//Code for yes Button
MBProgressHUD *hud = [MBProgressHUD showHUDAddedTo:self.navigationController.view animated:YES];
hud.mode = MBProgressHUDModeText;
hud.labelText = #"Succesfully displayed Second Message";
hud.margin = 10.f;
hud.yOffset = 150.f;
hud.removeFromSuperViewOnHide = YES;
[hud hide:YES afterDelay:3];
}
}
}
Can anyone help in finding the issue. Why I cannot get my second alert after clicking yes button in first alert?
You have not set the delegate for your UIAlertView and also make sure your delegate conforms to UIAlertViewDelegate protocol. Find the code snippet below.
You controller conforms to UIAlertViewDelegate protocol:
#interface YourViewController : UIViewController <UIAlertViewDelegate>
Create UIAlertView and set the deleagte:
UIAlertView *alertView = [[UIAlertView alloc] initWithTitle:#"First Message"
message:#"Show second message"
delegate:self
cancelButtonTitle:#"No"
otherButtonTitles:#"Yes", nil];
[alertView show];
Implement UIAlertViewDelegate delegate method:
- (void) alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex
{
if( 0 == buttonIndex ){ //cancel button
[alertView dismissWithClickedButtonIndex:buttonIndex animated:YES];
} else if ( 1 == buttonIndex ){
[alertView dismissWithClickedButtonIndex:buttonIndex animated:YES];
UIAlertView * secondAlertView = [[UIAlertView alloc] initWithTitle:#"Second Message"
message:#"Displaying second message"
delegate:nil
cancelButtonTitle:#"OK"
otherButtonTitles:nil];
[secondAlertView show];
}
}
You are specifying nil as the delegate for your alert views. You need to specify an object so the alertView:clickedButtonAtIndex: method can be called!
If you'd like to handle this more clear, you could use a block-based AlertView.
Create new file->Subclass of->UIAlertView
SuperAlertView.h
#import <UIKit/UIKit.h>
#class MySuperAlertView;
typedef void (^MySuperAlertViewBlock) (MySuperAlertView *alertView);
#interface MySuperAlertView : UIAlertView
- (instancetype) initWithTitle:(NSString *)title message:(NSString *)message;
- (void) addButtonWithTitle:(NSString *)buttonTitle block:(MySuperAlertViewBlock) block;
#end
SuperAlertView.m
#import "MySuperAlertView.h"
#interface MySuperAlertView()<UIAlertViewDelegate>
#property NSMutableArray *blocks;
#end
#implementation MySuperAlertView
- (instancetype)initWithTitle:(NSString *)title message:(NSString *)message
{
if (self = [super initWithTitle:title message:message delegate:self cancelButtonTitle:nil otherButtonTitles:nil])
{
self.blocks = [NSMutableArray array];
}
return self;
}
- (void)addButtonWithTitle:(NSString *)buttonTitle block:(MySuperAlertViewBlock)block
{
[self addButtonWithTitle:buttonTitle];
[self.blocks addObject:block ? [block copy] : [NSNull null]];
}
- (void)alertView:(UIAlertView *)alertView didDismissWithButtonIndex:(NSInteger)buttonIndex
{
MySuperAlertViewBlock block = self.blocks[buttonIndex];
if ((id) block != [NSNull null]){
block(self);
}
}
#end
Usage:
MySuperAlertView *alertView = [[MySuperAlertView alloc] initWithTitle:#"Info" message:NSLocalizedString(#"EMAIL_SENT_SUCCESSFULL", nil)];
[alertView addButtonWithTitle:#"Ok" block:^(MySupertAlertView *alertView) {
// handle result from ok button here
}];
[alertView addButtonWithTitle:#"cancel" block:NULL];
dispatch_async(dispatch_get_main_queue(), ^{
[alertView show];
});

How to call NSURL for Rate My App properly in xcode?

Im trying to put in a rate App alert, but its not opening properly, in fact when I click the Yes button it dosnt open the url at all, How do I make it work?
Below is the call in the viewdidload:
The below code in viewdidload is also in app delegate
[super viewDidLoad];
//rate app
NSUserDefaults *prefsr = [NSUserDefaults standardUserDefaults];
NSInteger launchCount = [prefsr integerForKey:#"launchCount"];
if (launchCount >= 1) {
UIAlertView *alertRate = [[UIAlertView alloc] initWithTitle:#"Like this app?" message:#"Rate on the app store." delegate:nil cancelButtonTitle:#"No, thanks" otherButtonTitles:#"Yes",#"Remind me later", nil];
[alertRate show];
}
-(void)alertViewRate:(UIAlertView *)alertViewRate clickedButtonAtIndex:(NSInteger)buttonIndexP{
if (buttonIndexP == 2) {
[[UIApplication sharedApplication] openURL:[NSURL URLWithString:#"https://www.google.com"]];
}
else if (buttonIndexP == 1){
[[UIApplication sharedApplication] openURL:[NSURL URLWithString:#"https://www.google.com"]];
}
Add the delegate Self while creating the UIAlertView
UIAlertView *alertRate = [[UIAlertView alloc] initWithTitle:#"Like this app?" message:#"Rate on the app store." delegate:self cancelButtonTitle:#"No, thanks" otherButtonTitles:#"Yes",#"Remind me later", nil];
[alertRate show];
Delegate method for UIAlertView also need to be chnaged
-(void)alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex
{
if (buttonIndex == 2) {
[[UIApplication sharedApplication] openURL:[NSURL URLWithString:#"https://www.google.com"]];
}
else if (buttonIndex == 1){
[[UIApplication sharedApplication] openURL:[NSURL URLWithString:#"https://www.google.com"]];
}
}
You haven't set the delegate to self while you are creating UIAlertView object. So -(void)alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex method is not getting called when you click on buttons in the AlertView.
Change your code From
UIAlertView *alertRate = [[UIAlertView alloc] initWithTitle:#"Like this app?" message:#"Rate on the app store." delegate:nil cancelButtonTitle:#"No, thanks" otherButtonTitles:#"Yes",#"Remind me later", nil];
To
UIAlertView *alertRate = [[UIAlertView alloc] initWithTitle:#"Like this app?" message:#"Rate on the app store." delegate:self. cancelButtonTitle:#"No, thanks" otherButtonTitles:#"Yes",#"Remind me later", nil];

how to get UIAlertView otherButtons' title?

-(void) alertView: (UIAlertView *) alertVw clickedButtonAtIndex:(NSInteger)buttonIndex
{
NSString *str = [NSString stringWithFormat:#"%d 。", buttonIndex];
UIAlertView *newAlertVw = [[UIAlertView alloc] initWithTitle:#"INFO" message:str delegate:nil cancelButtonTitle:#"OK" otherButtonTitles:nil];
[newAlertVw show];
}
UIAlertView *alertVw = [[UIAlertView alloc] initWithTitle:#"TITLE"
message:#"box message"
delegate:self
cancelButtonTitle:#"hide"
otherButtonTitles:#"T1", #"T2", nil];
[alertVw show];
if user touched the one of otherButtons, yes, we know which button that user touched.
then, how can I to get the title of button that user just touched?
thanks.
With the delegate on your UIAlertView set to self you can use this:
-(void)alertView:(UIAlertView*)alertView didDismissWithButtonAtIndex:(NSInteger)buttonIndex
{
NSLog(#"Button Title -----> %#",[alertView buttonTitleAtIndex:buttonIndex]);
// or you can check if it equals to string
if([[alertView buttonTitleAtIndex:buttonIndex]isEqual:#"Enter"])
{
// your code goes here
}
}
The method is buttonTitleAtIndex: and the info is simply found on the UIAlertView doc page.
https://developer.apple.com/library/ios/documentation/uikit/reference/UIAlertView_Class/UIAlertView/UIAlertView.html#//apple_ref/occ/instm/UIAlertView/buttonTitleAtIndex:
I advise you to refer to the doc as often as you can this is how you will learn and remember.
Actually, if you want to use the delegate method you have to use -(void)alertView:(UIAlertView *)alertView didDismissWithButtonIndex:(NSInteger)buttonIndex.
and not
-(void)alertView:(UIAlertView*)alertView didDismissWithButton At Index:(NSInteger)buttonIndex
see: https://developer.apple.com/library/ios/documentation/uikit/reference/UIAlertViewDelegate_Protocol/UIAlertViewDelegate/UIAlertViewDelegate.html#//apple_ref/occ/intfm/UIAlertViewDelegate/alertView:didDismissWithButtonIndex:
I guess it was just a typo of Pancho.

AlertView inside alertView

A button from the first alert executes the second alert which is basically a confirmation to call someone. I don't get any errors but it doesn't work.
When I press on the second alert the Call button it crashes
-(void) alertView: (UIAlertView *) alertView clickedButtonAtIndex:(NSInteger)buttonIndex
{
NSString *buttonString = [alertView buttonTitleAtIndex:buttonIndex];
if ([buttonString isEqualToString:#"Phone"])
{
UIAlertView *alert2 = [[UIAlertView alloc] initWithTitle:#"Notice!" message:#"You are about to call .... Do you wish to continue?" delegate:nil cancelButtonTitle:#"Cancel" otherButtonTitles:#"Call", nil];
[alert2 show];
if ([buttonString isEqualToString:#"Call"]){
[[UIApplication sharedApplication] openURL:[NSURL URLWithString:[NSString stringWithFormat:#"tel://12345678"]]];
}
if([buttonString isEqualToString:#"Website"]){
[[UIApplication sharedApplication] openURL:[NSURL URLWithString:#"website"]];
}
if ([buttonString isEqualToString:#"Facebook"]){
[[UIApplication sharedApplication] openURL:[NSURL URLWithString:#"http://www.facebook.com/groups/..../"]];
}
}
}
You assigned to delegate as a nil in second alertview. That's why the alertView:clickedButtonAtIndex: delegate method is not called in second time. So you should assign to delegate as a self.
-(void) alertView: (UIAlertView *) alertView clickedButtonAtIndex:(NSInteger)buttonIndex {
NSString *buttonString = [alertView buttonTitleAtIndex:buttonIndex];
if([buttonString isEqualToString:#"Phone"]) {
UIAlertView *alert2 = [[UIAlertView alloc] initWithTitle:#"Notice!" message:#"You are about to call .... Do you wish to continue?" delegate:self cancelButtonTitle:#"Cancel" otherButtonTitles:#"Call", nil];
[alert2 show];
}
if([buttonString isEqualToString:#"Call"]){
[[UIApplication sharedApplication] openURL:[NSURL URLWithString:[NSString stringWithFormat:#"tel://12345678"]]];
}
if([buttonString isEqualToString:#"Website"]){
[[UIApplication sharedApplication] openURL:[NSURL URLWithString:#"website"]];
}
if([buttonString isEqualToString:#"Facebook"]){
[[UIApplication sharedApplication] openURL:[NSURL URLWithString:#"http://www.facebook.com/groups/..../"]];
}
}
I think it will be helpful to you.
Launch the second alert with a delay. Problem is - alert needs some time to hide itself, before any new alerts can appear, thus directly calling second one will not work.
try this for example:
- (void) alertView: (UIAlertView *) alertView clickedButtonAtIndex:(NSInteger)buttonIndex {
NSString *buttonString = [alertView buttonTitleAtIndex:buttonIndex];
if ([buttonString isEqualToString:#"Phone"])
{
[self performSelector:#selector(callSecondAlertView) withObject:nil afterDelay:1];
}
}
- (void)callSecondAlertView
{
//show new alert view
UIAlertView *alert2 = [[UIAlertView alloc] initWithTitle:#"Notice!" message:#"You are about to call .... Do you wish to continue?" delegate:nil cancelButtonTitle:#"Cancel" otherButtonTitles:#"Call", nil];
[alert2 show];
[alert2 release];
}
If not working, or too long delay - play with afterDelay: value.
got it! for some reason the [[[alertView subviews] objectAtIndex:6] setBackgroundColor... for the fisrt alertView buttons i had interferes with the second alertView functionality. xcode though did not direct me directly to the problem until i updated xcode to 4.4.1
Use the alertView:didDismissWithButtonIndex: delegate method instead of the alertView:clickedButtonAtIndex: delegate method. The former is called after the alert is gone. This make more sense when you want to show a second based on the tapped button of the first alert view.
Also give tag to UIAlertview objects.

Iphone presentLocalNotificationNow not triggering alert and sound while app in background

I have an App registers for location updates, running tests, sometime when I enter a region while the app is in the background I receive a alarm notification with sound. sometime I only see the notification in notification center, and i did not receive any sound and alert...
What can you do to always get the sound and the alert notification ?
this is what i have in my view
UILocalNotification *localNotif = [[UILocalNotification alloc] init];
localNotif.fireDate = nil;
localNotif.hasAction = YES;
localNotif.alertBody = fbName;
localNotif.alertAction = #"View";
localNotif.soundName = UILocalNotificationDefaultSoundName;
[[UIApplication sharedApplication]presentLocalNotificationNow:localNotif];
and this is the app delegate
- (void)application:(UIApplication *)application didReceiveLocalNotification (UILocalNotification *)notification
{
if (notification)
{
UIAlertView *alertView = [[UIAlertView alloc] initWithTitle:#"Alert"
message:notification.alertBody
delegate:self cancelButtonTitle:#"OK"
otherButtonTitles:nil];
[alertView show];
}
}
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{
facebook = [[Facebook alloc] initWithAppId:kAppId andDelegate:self];
UILocalNotification *notification = [launchOptions objectForKey:UIApplicationLaunchOptionsLocalNotificationKey];
if (notification)
{
UIAlertView *alertView = [[UIAlertView alloc] initWithTitle:#"Alert"
message:notification.alertBody
delegate:self cancelButtonTitle:#"OK"
otherButtonTitles:nil];
[alertView show];
}
return YES;
}
If the application is running in the background, the local notification will not get an alert or sound, as it is directly received by your application. In that case, you need to present the notification using presentLocalNotificationNow.
- (void)application:(UIApplication *)application didReceiveLocalNotification:(UILocalNotification *)notification
{
UIApplicationState applicationState = application.applicationState;
if (applicationState == UIApplicationStateBackground) {
[application presentLocalNotificationNow:notification];
}
}

Resources