i know this question had been asked before Show AlertView dialog from UILocalNotification but due to unsatisfied answers and different scenarios i mention my question what i am trying to do is to check if the state of the application is in the foreground the app should display an alertView or alertControl with actions and Creates a notification with a given name, sender, and information and posts it to the receiver, here we put our self in front of two solution
First Solution:
to use the alert view with his delegate method like How to create an alert box in iphone?
UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"Wait" message:@"Are you sure you want to delete this. This action cannot be undone" delegate:self cancelButtonTitle:@"Delete" otherButtonTitles:@"Cancel", nil];
[alert show];
//That will display the message.
//Then to check whether they tapped delete or cancel, use this:
- (void)alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex{
if (buttonIndex == 0){
//delete it
}
}
or using the Second Solution:
// we need to show alertDialogue //
UIAlertController* alert = [UIAlertController
alertControllerWithTitle:@"Reminder"
message:notification.alertBody
preferredStyle:UIAlertControllerStyleAlert];
UIAlertAction *cancelAction = [UIAlertAction actionWithTitle:@"Cancel" style:UIAlertActionStyleDefault
handler:^(UIAlertAction * action) {
}];
UIAlertAction *viewDetailsAction = [UIAlertAction actionWithTitle:@"View Details" style:UIAlertActionStyleDefault
handler:^(UIAlertAction * action) {
reservationId = (NSNumber *)[notification.userInfo valueForKey:@"reservationId"];
[[NSNotificationCenter defaultCenter] postNotificationName:@"pushNotification" object:nil userInfo:notification.userInfo];
}];
[alert addAction:viewDetailsAction];
[alert addAction:cancelAction];
the full code to understand my problem :
-(void)application:(UIApplication *)application didReceiveLocalNotification:(UILocalNotification *)notification
{
if (application.applicationState == UIApplicationStateActive)
{
// we need to show alertDialogue //
UIAlertController* alert = [UIAlertController alertControllerWithTitle:@"Reminder"
message:notification.alertBody
preferredStyle:UIAlertControllerStyleAlert];
UIAlertAction *cancelAction = [UIAlertAction actionWithTitle:@"Cancel" style:UIAlertActionStyleDefault
handler:^(UIAlertAction * action) {
}];
UIAlertAction *viewDetailsAction = [UIAlertAction actionWithTitle:@"View Details" style:UIAlertActionStyleDefault
handler:^(UIAlertAction * action) {
reservationId = (NSNumber *)[notification.userInfo valueForKey:@"reservationId"];
[[NSNotificationCenter defaultCenter] postNotificationName:@"pushNotification" object:nil userInfo:notification.userInfo];
}];
[alert addAction:viewDetailsAction];
[alert addAction:cancelAction];
// [self presentViewController:alert animated:YES completion:nil];
//this line cause not visible interface inside appdelegate//
}
else
{
reservationId = (NSNumber *)[notification.userInfo valueForKey:@"reservationId"];
[[NSNotificationCenter defaultCenter] postNotificationName:@"pushNotification" object:nil userInfo:notification.userInfo];
}
}
as you can see i am checking on the didRecieveLocalNotification method if the app is in the foreground or in whatever different state is. if the app inside the background create a local notification with name and sender if is the foreground create alertController or alertView with a local notification with name and sender
and i could not use the first solution because its delegate method and that will prevent me from accessing the userInfo of the local notification and for the second solution i could not use it due to error that said no visible inferface for AppDelegate declare the selector presentViewController:animated:completed
thanks in advance and Happy Coding :D