How implement scheduling event with post a message on fb - iphone

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.

Related

Get New Location when App Enters Foreground

I am having difficulty implementing what I think should be a pretty simple capability. I have an button that compares the user's location to another set of lat/lon coordinates and displays an alert telling the user if they are "nearby" or "far away" from the location. This works well if the app remains in the foreground.
However, if the app gets sent to the background, the locationManager stops updating the user's location. If this happens, when the user brings the app back up and presses the "compare location button", the app doesn't seem to be able to get the user's new coordinates correctly.
Adding receive location updates to the background modes seems to work, but I know this will drain battery an may be rejected by Apple.
All I really want is to be able to have the button wait for an accurate set of lat/lon coordinates before trying to calculate the distance between the user and the other set lat/lon coordinates. Here is what I have so far:
The Location Button Does This:
-(void) locationButton {
NSString *userLatitude =[(PDCAppDelegate *)[UIApplication sharedApplication].delegate
getUserLatitude];
NSString *userLongitude =[(PDCAppDelegate *)[UIApplication sharedApplication].delegate
getUserLongitude];
NSString *placeLatitude = [[NSUserDefaults standardUserDefaults]
stringForKey:#"savedLatitude"];
NSString *placeLongitude = [[NSUserDefaults standardUserDefaults]
stringForKey:#"savedLongitude"];
NSString *distanceURL = [NSString stringWithFormat:#"http://www.website.com/page.php?
lat1=%#&lon1=%#&lat2=%#&lon2=%#",userLatitude, userLongitude, placeLatitude,
placeLongitude];
NSData *distanceURLResult = [NSData dataWithContentsOfURL:[NSURL
URLWithString:distanceURL]];
NSString *distanceInFeet = [[NSString alloc] initWithData:distanceURLResult
encoding:NSUTF8StringEncoding];
if ([distanceInFeet isEqualToString:#"1"]) {
UIAlertView *alert = [[UIAlertView alloc] initWithTitle:#"You're Near!"
message:#"" delegate:self cancelButtonTitle:#"OK" otherButtonTitles:nil, nil];
[alert show];
return;
}
if ([distanceInFeet isEqualToString:#"0"]) {
UIAlertView *alert = [[UIAlertView alloc] initWithTitle:#"You're Far!" message:#""
delegate:self cancelButtonTitle:#"OK" otherButtonTitles:nil, nil];
[alert show];
return;
}
}
The first few lines of the locationButton method get the userLatitude and userLongitude from my App Delegate:
- (NSString *)getUserCoordinates {
NSString *userCoordinates = [NSString stringWithFormat:#"latitude: %f longitude: %f",
locationManager.location.coordinate.latitude,
locationManager.location.coordinate.longitude];
locationManager = [[CLLocationManager alloc] init];
locationManager.distanceFilter = kCLDistanceFilterNone; // whenever we move
locationManager.desiredAccuracy = kCLLocationAccuracyHundredMeters; // 100 m
[locationManager startUpdatingLocation];
return userCoordinates;
}
- (NSString *)getUserLatitude {
NSString *userLatitude = [NSString stringWithFormat:#"%f",
locationManager.location.coordinate.latitude];
return userLatitude;
}
- (NSString *)getUserLongitude {
NSString *userLongitude = [NSString stringWithFormat:#"%f",
locationManager.location.coordinate.longitude];
return userLongitude;
}

Button not showing in nslocalnotification?

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];
}
}

How to On/Off UILocalNotification in iphone?

I have set UILocalNotification according UISWitch position by calling following method on button action set alarm.
- (void)scheduleNotificationWithInterval:(int)minutesBefore {
NSDateFormatter* theDateFormatter = [[NSDateFormatter alloc] init];
[theDateFormatter setFormatterBehavior:NSDateFormatterBehavior10_4];
[theDateFormatter setDateFormat:#"EEEE"];
NSString *currentDay= [theDateFormatter stringFromDate:combo2.datePick.date];
[[UIApplication sharedApplication] cancelAllLocalNotifications];
UILocalNotification *localNotif = [[UILocalNotification alloc] init];
if (localNotif == nil)
return;
localNotif.fireDate = [combo2.datePick.date dateByAddingTimeInterval:-30];
localNotif.timeZone = [NSTimeZone defaultTimeZone];
localNotif.repeatInterval=NSCalendarCalendarUnit;
localNotif.repeatInterval = NSDayCalendarUnit;
if (iBOfSwitchAlarm.on) {
if([combo1.selectedText isEqualToString:currentDay])
{
NSLog(#"In DayCheck");
localNotif.alertBody = #"Alarm Test";
localNotif.alertAction = #"Ok";
localNotif.soundName = UILocalNotificationDefaultSoundName;
NSDictionary *infoDict = [NSDictionary dictionaryWithObject:#"THere Message for Alarm" forKey:#"Message"];
localNotif.userInfo = infoDict;
[[UIApplication sharedApplication] scheduleLocalNotification:localNotif];
}
}else{
NSLog(#"SwOff");
}
}
I write following code in AppDelegate. Here also i check switch position.
ViewController *obj =[[ViewController alloc]init];
NSString *itemName = [notif.userInfo objectForKey:#"Message"];
if (obj.iBOfSwitchAlarm.on) {
UIAlertView *alertView = [[UIAlertView alloc] initWithTitle:#"Alarm Title" message:itemName delegate:self cancelButtonTitle:#"Dismiss" otherButtonTitles:#"Continue", nil];
alertView.tag = 1111;
[alertView show];
app.applicationIconBadgeNumber = notif.applicationIconBadgeNumber-1;
}else{
NSLog(#"SwitchOff");
}
Now my problem is when I set Notification according to time. It is getting alert on time. But in between when i make Switch OFF and again ON it is not getting alert. There is any way to make On and Off notification according to switch position. Please help me if any one knows. Thanks in advance.
to turn off you notification you can use [[UIApplication sharedApplication] cancelAllLocalNotifications]; or for specific [[UIApplication sharedApplication] cancelLocalNotification:<#(UILocalNotification *)#>];
and then you will have to reschedule your notification like this
[[MKLocalNotificationsScheduler sharedInstance] scheduleNotificationOn:[[NSDate Date] addTimeInterval:60*60*24*1]
text:#"Hi this is notification"
action:#"View"
sound:#"Raising_Sound.mp3"
launchImage:nil
andInfo:nil];
In my case i am rescheduling for one day later
If there can only be one UILocalNotification at a time, I would declare it as a variable so you can access it at will.
If you store your UILocalNotification you can use a combination of -cancelLocalNotification: to turn it OFF and -scheduleLocalNotification: to turn it back ON, while keeping the properties of it intact.

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.

Programmatically add reminder to iPhone calendar?

Can anyone have any idea about programmatically add reminder to iPhone calendar.
I searched the iPhone SDK documentation but didn't find anything.
Thanks and Regards
Jayaraj
This functionality is available in iOS 4.x with EventKit.
You could try to read this EventKit sample.
Oh yes you can...
#import <EventKit/EventKit.h>
...
EKEventStore *eventDB = [[EKEventStore alloc] init];
EKEvent *myEvent = [EKEvent eventWithEventStore:eventDB];
myEvent.title = #"New Event";
myEvent.startDate = [[NSDate alloc] init];
myEvent.endDate = [[NSDate alloc] init];
myEvent.allDay = NO;
myEvent.notes = #"Test";
[myEvent setCalendar:[eventDB defaultCalendarForNewEvents]];
NSError *err;
[eventDB saveEvent:myEvent span:EKSpanThisEvent error:&err];
if (err == noErr) {
UIAlertView *alert = [[UIAlertView alloc]
initWithTitle:#"Event Created"
message:#"Yay!?"
delegate:nil
cancelButtonTitle:#"Okay"
otherButtonTitles:nil];
[alert show];
[alert release];
}
You can't. If you need this functionality you should file a bug with Apple explaining why you need it.