Button not showing in nslocalnotification? - iphone

I have written the following code for displaying local notifications.However,a button is not showing up on the notifications when i receive them.Can anybody tell me what I am doing wrong?
I have written the following piece of code in application did enter background event.
UILocalNotification *local=[[UILocalNotification alloc]init];
NSDate *alertTime = [[NSDate date] dateByAddingTimeInterval:10];
local.fireDate=alertTime;
local.timeZone=[NSTimeZone defaultTimeZone];
local.alertBody=#"Hello this is a local notif";
local.alertAction=#"Show";
local.repeatInterval=0;
local.applicationIconBadgeNumber=1;
local.soundName=UILocalNotificationDefaultSoundName;
UIApplication *abc=[UIApplication sharedApplication];
[abc scheduleLocalNotification:local];

You have to write your code in AppDelegate.m File in below method.
- (void)application:(UIApplication *)application didReceiveLocalNotification:(UILocalNotification *)notification
{
if (application.applicationState == UIApplicationStateActive) {
UIAlertView *alert = [[UIAlertView alloc] initWithTitle:nil message:notification.alertBody delegate:nil cancelButtonTitle:nil otherButtonTitles:#"Show", nil];
[alert show];
[alert release];
}
}

Related

Alert with instructions before gameplay starts

How can I add an alert that displays instructions before game starts:
See code below:
- (void)viewDidLoad
{
[super viewDidLoad];
if (questions && configDictionary) {
[questionLabel setText:[[questions objectAtIndex:currentQuestonIndex] objectForKey:#"question"]];
NSArray *answers = [[questions objectAtIndex:currentQuestonIndex] objectForKey:#"answers"];
[answerLabel0 setText:[answers objectAtIndex:0]];
[answerLabel1 setText:[answers objectAtIndex:1]];
[answerLabel2 setText:[answers objectAtIndex:2]];
[answerLabel3 setText:[answers objectAtIndex:3]];
[pointsPerAnswerLabel setText:[NSString stringWithFormat:#"+%d points", [[configDictionary objectForKey:kPointsPerCorrectAnswer] intValue]]];
[currentQuestionNumberLabel setText:[NSString stringWithFormat:#"question %d", currentQuestonIndex+1]];
}
}
Use a UIAlertView:
UIAlertView *alert = [[UIAlertView alloc] initWithTitle:#"Instructions"
message:#"Your Instructions..." delegate:self cancelButtonTitle:#"Dismiss"
otherButtonTitles:nil, nil];
[alert show];
If you want to alert the user every time the app launches place it in the
- (void)applicationDidFinishLaunching:(UIApplication *)application {
}
Edit
You said you wanted to start the game after the dismiss button is pressed. So take advantage of the UIAlertView delegate:
- (void)alertView:(UIAlertView *)alertView didDismissWithButtonIndex:(NSInteger)buttonIndex {
if (buttonIndex == 0){
//Start your game!
}
}
UIAlertView *message = [[UIAlertView alloc] initWithTitle:#"How to play"
message:#"Answer the questions correctly to get points blablabla..."
delegate:nil
cancelButtonTitle:#"OK"
otherButtonTitles:nil];
[message show];

iPhone iOS 5 SDK in app SMS error

thanks in advance every time I call to create an in app SMS i get the following error...
Application tried to push a nil view controller on target .
This is the code I have that worked in OS 4 SDK...
MFMailComposeViewController *MailController;
MFMessageComposeViewController *SMScontroller;
NSError *error;
NSString *EmailMessage;
NSString *SubjectMessage;
-(IBAction)sendInAppSMS
{
if([MFMessageComposeViewController canSendText])
{
SMScontroller = [[MFMessageComposeViewController alloc] init];
SMScontroller.messageComposeDelegate = self;
NSString *MessageString = #"Hello";
SMScontroller.body = MessageString;
SMScontroller.navigationBar.tintColor = [UIColor blackColor];
[[UIApplication sharedApplication] setStatusBarStyle:UIStatusBarStyleBlackOpaque];
[self presentModalViewController:SMScontroller animated:YES];
[[UIApplication sharedApplication] setStatusBarStyle:UIStatusBarStyleBlackOpaque];
}
else{
// alertView to tell user to setup a mail account.
NSString *message = [[NSString alloc] initWithFormat:#"To use this feature, you need to have an iPhone with SMS abilities."];
UIAlertView *alert = [[UIAlertView alloc] initWithTitle:#"SMS Account Not Detected" message:message delegate:nil cancelButtonTitle:#"Understood" otherButtonTitles:nil];
[alert show];
}
}
- (void)messageComposeViewController:(MFMessageComposeViewController *)controller didFinishWithResult:(MessageComposeResult)result
{
switch (result) {
case MessageComposeResultCancelled:
{
UIAlertView *alert = [[UIAlertView alloc] initWithTitle:#"I Like It" message:#"User cancelled sending the SMS"
delegate:self cancelButtonTitle:#"OK" otherButtonTitles: nil];
[alert show];
}
break;
case MessageComposeResultFailed:
{
UIAlertView *alert = [[UIAlertView alloc] initWithTitle:#"I Like It" message:#"Error occured while sending the SMS"
delegate:self cancelButtonTitle:#"OK" otherButtonTitles: nil];
[alert show];
}
break;
case MessageComposeResultSent:
{
UIAlertView *alert = [[UIAlertView alloc] initWithTitle:#"I Like It" message:#"SMS sent successfully..!"
delegate:self cancelButtonTitle:#"OK" otherButtonTitles: nil];
[alert show];
}
break;
default:
break;
}
[self dismissModalViewControllerAnimated:YES];
}
I believe if you are running this in the simulator you'll get the error since it doesn't have sms. I get the same error in simulator but if i connect to my phone it works fine.

How implement scheduling event with post a message on fb

I am creating an application in which I have add a facility for post message on ur account. Now this facility I am adding an event for scheduling. With help of that user can write a message and post that later or on particular date and time. For this I used a local notification event which is generate on given date by user. But problem is that when notification generate then I have call a function which is used for post message on Facebook. For generate notification I have used this code:
-(IBAction)save{
NSString *str1=[NSString stringWithFormat:#"%#",txt_date.text];
NSString *str2=[NSString stringWithFormat:#" %#",txt_time.text];
str1=[str1 stringByAppendingFormat:str2];
selected_label.text= str1;
[[UIApplication sharedApplication] cancelAllLocalNotifications];
NSDate *today=[NSDate date];
NSDateFormatter* formatter_current = [[[NSDateFormatter alloc] init] autorelease];
formatter_current.timeZone = [NSTimeZone timeZoneWithAbbreviation:#"GMT"];
//Set the required date format
[formatter_current setDateFormat:#"yyyy-MM-dd hh:mm a"];
NSLog(#"current date is =%#",str1);
today=[formatter_current dateFromString:str1];
NSLog(#"current date:-%#",today);
UILocalNotification* ln = [[UILocalNotification alloc] init];
ln.alertBody = #"Wake Up Sid";
ln.applicationIconBadgeNumber = 1;
ln.fireDate = today; //[NSDate dateWithTimeIntervalSinceNow:15];
ln.timeZone = [NSTimeZone timeZoneWithAbbreviation:#"GMT"];
NSLog(#"alarm will activate on%#",today);
NSDateFormatter* formatter_alarm = [[[NSDateFormatter alloc] init] autorelease];
NSLocale *uslocale = [[NSLocale alloc] initWithLocaleIdentifier:#"en_US"];
[formatter_alarm setLocale:uslocale];
[uslocale release];
formatter_alarm.timeZone = [NSTimeZone timeZoneWithAbbreviation:#"GMT"];
[formatter_alarm setDateFormat:#"hh:mm a"];
NSString *str=[formatter_alarm stringFromDate:today];
NSLog(#"%#",str);
ln.alertBody = [NSString stringWithFormat:#"Your first appointment at %#",str];
ln.soundName = UILocalNotificationDefaultSoundName;
ln.repeatInterval=NSDayCalendarUnit;
[[UIApplication sharedApplication] scheduleLocalNotification:ln];
[ln release];
}
and in appdelegate file I use this function for received notification and call post message function:
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions{
// Override point for customization after application launch.
self.viewController=[[demo_social_updatesViewController alloc]initWithNibName:#"demo_social_updatesViewController" bundle:nil];
nav_controller=[[UINavigationController alloc] initWithRootViewController:self.viewController];
// Add the view controller's view to the window and display.
[self.window addSubview:nav_controller.view];
[self.window makeKeyAndVisible];
appDelegate_acess_token=[[NSUserDefaults standardUserDefaults] stringForKey:#"access_token"];
application.applicationIconBadgeNumber = 0;
// Handle launching from a notification
UILocalNotification *localNotif =
[launchOptions objectForKey:UIApplicationLaunchOptionsLocalNotificationKey];
if (localNotif) {
NSLog(#"Recieved Notification %#",localNotif);
}
return YES;
}
- (void)applicationDidEnterBackground:(UIApplication *)application{
if (application.applicationIconBadgeNumber == 1) {
BOOL tmp=[Global_Class_parsing post_comment_fb:appDelegate_acess_token uesr_comment:#"testing message111"];
if(tmp){
UIAlertView *av = [[[UIAlertView alloc] initWithTitle:#"Sucessfully posted to photos & wall!"
message:#"Check out your Facebook to see!"
delegate:nil
cancelButtonTitle:#"OK"
otherButtonTitles:nil] autorelease];
[av show];
}
else{
UIAlertView *av = [[[UIAlertView alloc] initWithTitle:#"error"message:#"Check connection!"
delegate:nil
cancelButtonTitle:#"OK"
otherButtonTitles:nil] autorelease];
[av show];
}
}
/*
Use this method to release shared resources, save user data, invalidate timers, and store enough application state information to restore your application to its current state in case it is terminated later.
If your application supports background execution, this method is called instead of applicationWillTerminate: when the user quits.
*/
}
- (void)applicationWillEnterForeground:(UIApplication *)application{
/*
Called as part of the transition from the background to the active state; here you can undo many of the changes made on entering the background.
*/
if (application.applicationIconBadgeNumber == 1) {
BOOL tmp=[Global_Class_parsing post_comment_fb:appDelegate_acess_token uesr_comment:#"testing message111"];
if(tmp){
UIAlertView *av = [[[UIAlertView alloc] initWithTitle:#"Sucessfully posted to photos & wall!"
message:#"Check out your Facebook to see!"
delegate:nil
cancelButtonTitle:#"OK"
otherButtonTitles:nil] autorelease];
[av show];
}
else{
UIAlertView *av = [[[UIAlertView alloc] initWithTitle:#"error"message:#"Check connection!"
delegate:nil
cancelButtonTitle:#"OK"
otherButtonTitles:nil] autorelease];
[av show];
}
}
}
- (void)application:(UIApplication *)application didReceiveLocalNotification:(UILocalNotification *)notification {
UIApplicationState state = [application applicationState];
if (state == UIApplicationStateInactive) {
NSLog(#"Recieved Notification %#",notification);
} else {
NSLog(#"Recieved Notification method call.");
}
}
Now problem is that when notification generate and applicationbadge number become 1 then it not call any function and my post message function not calling. So how I fix that error?
I get solution for this question is that i have two way to implement scheduling with post method one is simply use notification. And another is use web-service. I have used both and working well.

How to add events in iPhone using Event Kit framework

I am making a Restaurant application in which i need to add events in calendar, events information are coming from server, if client add any event it should show in calendar for that specified date, i have used event kit frame work and successfully added one event into the calendar, however how to add multiple events in calendar using event kit.
First of All, Add EventKit Framework and import it in .h file. Like below.
#import <EventKit/EventKit.h>
See Below Function and Modify it as per your need.
-(IBAction)AddEvent:(id)sender{
EKEventStore *eventSotre = [[EKEventStore alloc] init];
EKEvent *event = [EKEvent eventWithEventStore:eventSotre];
if([txtTitle.text isEqualToString:#""] || txtTitle.text == NULL)
txtTitle.text=#"Event Title";
event.title= txtTitle.text;
NSDate *duedate = pkrDate.date;
event.startDate =duedate;
event.endDate= [[NSDate alloc] initWithTimeInterval:600 sinceDate:duedate];
if(switchAlarm.on==TRUE){
NSArray *arrAlarm = [NSArray arrayWithObject:[EKAlarm alarmWithAbsoluteDate:duedate]];
event.alarms= arrAlarm;
}
[event setCalendar:[eventSotre defaultCalendarForNewEvents]];
NSError *err;
BOOL isSuceess=[eventSotre saveEvent:event span:EKSpanThisEvent error:&err];
if(isSuceess){
UIAlertView *alertview = [[UIAlertView alloc] initWithTitle:#"Event" message:#"Event added in calendar" delegate:self cancelButtonTitle:#"OK" otherButtonTitles:nil];
[alertview show];
[alertview release];
}
else{
UIAlertView *alertview = [[UIAlertView alloc] initWithTitle:#"Event" message:[err description] delegate:self cancelButtonTitle:#"OK" otherButtonTitles:nil];
[alertview show];
[alertview release];
}
[eventSotre release];
}
Please modify it as per your need. I just paste it from my one of app code.

Detecting which UIAlertView was clicked

i need to pop up alert when my application loaded... I called it didfinished launching..
after clicking ok button need to show another alert message i use clickedButtonAtIndex...
Now when I clicked the ok button its calling again and again.. the alertview..
I need to call only once... what to do?
-(BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
// Override point for customization after application launch.
// Add the tab bar controller's view to the window and display.
[window addSubview:tabBarController.view];
[window makeKeyAndVisible];
viewControllersList = [[NSMutableArray alloc] init];
UIAlertView *alert = [[UIAlertView alloc] initWithTitle:#"Alert" message:#"Alow this app to use your GPS location"
delegate:self cancelButtonTitle:#"No" otherButtonTitles:#"Yes", nil];
[alert show];
[alert release];
}
- (void)alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex{
if (buttonIndex==0) {
NSLog(#"NO");
}
else {
NSLog(#"Yes");
UIAlertView *alert = [[UIAlertView alloc] initWithTitle:#"Alert" message:#"Do you want's to receive Push messages."
delegate:self cancelButtonTitle:#"No" otherButtonTitles:#"Yes", nil];
[alert show];
[alert release];
}
}
#thanks in advance.
set delegate:nil in second alertView
I mean
if (buttonIndex==0) { NSLog(#"NO"); } else {
NSLog(#"Yes");
UIAlertView *alert = [[UIAlertView alloc] initWithTitle:#"Alert" message:#"Do you want's to receive Push messages."
delegate:nil cancelButtonTitle:#"No" otherButtonTitles:#"Yes", nil];
[alert show];
[alert release];
}
Define each UIAlertView and in the delegate look for which Alert to respond to:
-(void)alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex{
if(alert1) {
if (buttonIndex==0) {
NSLog(#"NO");
} else {
NSLog(#"Yes");
UIAlertView *alert = [[UIAlertView alloc] initWithTitle:#"Alert" message:#"Do you want's to receive Push messages." delegate:self cancelButtonTitle:#"No" otherButtonTitles:#"Yes", nil];
[alert show];
[alert release];
}
} else {
/* the second alertview using the same buttonIndex */
}
}
You can also add a tag to your AlertView and te check the tag later on the clickedButtonAtIndex method
UIAlertView* alert = [[UIAlertView alloc]initWithTitle:#"" message:#"All local datawill be erased. Erase local data?" delegate:self cancelButtonTitle:nil otherButtonTitles:#"Erase", #"Cancel",nil];
alert.tag =123; // added the tag so we can prevent other message boxes ok button to mix up
[alert show];
then
-(void)alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex{
if (buttonIndex ==0 && alertView.tag==123){
// Do what ever you wish
}
}
If what you want is to receive the location, just request it and the system will automatically show the message for you. Follow this example:
- (void)applicationDidFinishLaunching:(UIApplication *)application {
[window addSubview:tabBarController.view];
// Create a location manager instance to determine if location services are enabled. This manager instance will be
// immediately released afterwards.
CLLocationManager *manager = [[CLLocationManager alloc] init];
if (manager.locationServicesEnabled == NO) {
UIAlertView *servicesDisabledAlert = [[UIAlertView alloc] initWithTitle:#"Location Services Disabled" message:#"You currently have all location services for this device disabled. If you proceed, you will be asked to confirm whether location services should be reenabled." delegate:nil cancelButtonTitle:#"OK" otherButtonTitles:nil];
[servicesDisabledAlert show];
[servicesDisabledAlert release];
}
[manager release];
}
http://developer.apple.com/library/ios/#samplecode/LocateMe/Listings/Classes_AppDelegate_m.html%23//apple_ref/doc/uid/DTS40007801-Classes_AppDelegate_m-DontLinkElementID_4
Same for push notifications.
though u got many implementation solutions already ...but i think better practice would be to assign a tag to each of your AlertView and before detecting the button tapped check the tag of invoking AlertView.Hope this helps.
#Gerard:Though both location manager and push notification service raise system generated messages ,these can be checked off by users not to be shown again.So better HIG complaint method is application generating message whenever push or location manager are required.