EventKit event which is for 55 year from current date is not added correctly in iOS 6 - ios6

I want to add event for 55 year from current date.Using below code for iOS 6,event is added in calendar with event name "New Event" and event was set to all day.This is not correct as per my code.Code is working perfectly for events which are below 55 year from current date.
-(void)addEvent{
EKEventStore *es = [[EKEventStore alloc] init];
EKAuthorizationStatus authorizationStatus = [EKEventStore authorizationStatusForEntityType:EKEntityTypeEvent];
BOOL needsToRequestAccessToEventStore = (authorizationStatus == EKAuthorizationStatusNotDetermined);
if (needsToRequestAccessToEventStore) {
[es requestAccessToEntityType:EKEntityTypeEvent completion:^(BOOL granted, NSError *error) {
if (granted) {
[self loadAllEventsForStore:es];
} else {
NSLog(#"Not Granted");
}
}];
} else {
BOOL granted = (authorizationStatus == EKAuthorizationStatusAuthorized);
if (granted) {
[self loadAllEventsForStore:es];
} else {
NSLog(#"Not Granted");
}
}
}
-(void)loadAllEventsForStore:(EKEventStore*)store{
[self setEventForStore:store yearfromCurrentDate:55];
[self setEventForStore:store yearfromCurrentDate:56];
}
-(void)setEventForStore:(EKEventStore*)store yearfromCurrentDate:(int)year{
EKEvent *event = [EKEvent eventWithEventStore:store];
event.title = [NSString stringWithFormat:#"Event %d",eventid++];
NSLog(#"=========== %# ==============", event.title);
NSCalendar *calendar = [NSCalendar currentCalendar];
NSDateComponents *comps = [NSDateComponents new];
[comps setDay:05];
[comps setMonth:1];
[comps setYear:year];
NSDate *startDate = [calendar dateByAddingComponents:comps toDate:[NSDate date] options:0];
NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
[dateFormatter setDateFormat:#"dd-MM-yyyy"];
NSString *startDateString = [dateFormatter stringFromDate:startDate];
NSLog(#"Start Date = %#", startDateString);
event.startDate = startDate;
NSDate *endDate = [event.startDate dateByAddingTimeInterval:60*60];;
[dateFormatter setDateFormat:#"dd-MM-yyyy"];
NSString *endDateString = [dateFormatter stringFromDate:endDate];
NSLog(#"End Date date = %#", endDateString);
event.endDate = endDate;
[event setCalendar:[store defaultCalendarForNewEvents]];
NSError *err = nil;
BOOL save=[store saveEvent:event span:EKSpanThisEvent commit:YES error:&err];
if (save && err==nil) {
NSLog(#"Event %# is added sucessfully",event.title);
}else
NSLog(#"Event %# is not added",event.title);
NSLog(#"==================================");
}
screenshot of calendar app after adding events programmatically:
Console Output:
2014-01-22 15:13:08.789 TestApp[19094:907] =========== Event 0 ==============
2014-01-22 15:13:08.796 TestApp[19094:907] Start Date = 27-02-2069
2014-01-22 15:13:08.798 TestApp[19094:907] End Date date = 27-02-2069
2014-01-22 15:13:08.884 TestApp[19094:907] Event Event 0 is added sucessfully
2014-01-22 15:13:08.886 TestApp[19094:907] ==================================
2014-01-22 15:13:08.889 TestApp[19094:907] =========== Event 1 ==============
2014-01-22 15:13:08.893 TestApp[19094:907] Start Date = 27-02-2070
2014-01-22 15:13:08.896 TestApp[19094:907] End Date date = 27-02-2070
2014-01-22 15:13:08.923 TestApp[19094:907] Event Event 1 is added sucessfully
2014-01-22 15:13:08.924 TestApp[19094:907] ==================================

Related

Timer with Target Day to show Alert

I have an app written here is set a fixed reference Day,after this day i want to get an Alert !
with this code I set the date and time : here Day 3 (Thuesday) and hour 11.
this works great! with an Timer in viewDidload my navigationItem.title shows Day/Hour/Minutes to the target day...
now i will set a Timer to this Day/Time, when i open my app after Thuesday (Wednesday or Thursday) the timer is 0 and the Alert shows!
with :
NSString *currentTimestamp = [NSString stringWithFormat:#"%.f", [resetTime timeIntervalSince1970]];
_epochTimeer.text = currentTimestamp;
i convert the Target time to epoch Time for my timer!
- (void) resetTimer:(id)sender {
NSDate *today = [NSDate date];
NSCalendar *gregorian = [[NSCalendar alloc] initWithCalendarIdentifier:NSGregorianCalendar];
[gregorian setLocale:[NSLocale currentLocale]];
NSDateComponents *nowComponents = [gregorian components:NSYearCalendarUnit | NSWeekCalendarUnit | NSHourCalendarUnit | NSMinuteCalendarUnit | NSSecondCalendarUnit fromDate:today];
[nowComponents setWeekday:3]; //2 = Monday 3 di 4 = mi 5 = do 6 = fr 7 = sa
[nowComponents setWeek: [nowComponents week]+ 1 ]; //Next week [nowComponents week] + 1]
[nowComponents setHour:11]; //8a.m.
[nowComponents setMinute:0];
[nowComponents setSecond:0];
NSDate *resetTime = [gregorian dateFromComponents:nowComponents];
NSDateFormatter *df = [[NSDateFormatter alloc] init];
[df setDateFormat:#"dd-MM-YYYY HH:mm:ss"];
NSDate *date1 = today;
NSDate *date2 = resetTime;
NSTimeInterval interval = [date2 timeIntervalSinceDate:date1];
int days = (int)interval / 86400;
int hours = (interval / 3600)- (days * 24);
int minutes = ((interval / 60) - (days * 24 * 60) -(hours * 60));
int sekunden = (days * 86400) + (hours * 3600) +(minutes * 60);
NSString *timeDiff = [NSString stringWithFormat:#"%02d Tage : %02d Std : %02d Min",days, hours, minutes];
self.navigationItem.title = [NSString stringWithFormat:#"%#",timeDiff];
NSString *currentTimestamp = [NSString stringWithFormat:#"%.f", [resetTime timeIntervalSince1970]];
_epochTimeer.text = currentTimestamp;
NSLog(#"timer: %#",timeDiff);
NSLog(#"timer: %#",currentTimestamp);
}
how can I set my timer with this epoch time or the target Day/time?
i am a beginner with timer and NSDate and I do not understand how I can write the code!
or is there a completely different solution to get my Alert after the Target day when i open my APP!
Thanks Jürgen
Sorry for my bad English ..:-)
Thanks for Helping, i Add your Code and Set the localNotification.fireDate to resetTime:
- (void) resetTimer:(id)sender {
NSDate *today = [NSDate date];
NSCalendar *gregorian = [[NSCalendar alloc] initWithCalendarIdentifier:NSGregorianCalendar];
[gregorian setLocale:[NSLocale currentLocale]];
NSDateComponents *nowComponents = [gregorian components:NSYearCalendarUnit | NSWeekCalendarUnit | NSHourCalendarUnit | NSMinuteCalendarUnit | NSSecondCalendarUnit fromDate:today];
[nowComponents setWeekday:7]; //2 = Monday 3 di 4 = mi 5 = do 6 = fr 7 = sa
[nowComponents setWeek: [nowComponents week]]; //Next week [nowComponents week] + 1]
[nowComponents setHour:9]; //8a.m.
[nowComponents setMinute:25];
[nowComponents setSecond:0];
NSDate *resetTime = [gregorian dateFromComponents:nowComponents];
NSDateFormatter *df = [[NSDateFormatter alloc] init];
[df setDateFormat:#"dd-MM-YYYY HH:mm:ss"];
NSDate *date1 = today;
NSDate *date2 = resetTime;
NSTimeInterval interval = [date2 timeIntervalSinceDate:date1];
int days = (int)interval / 86400;
int hours = (interval / 3600)- (days * 24);
int minutes = ((interval / 60) - (days * 24 * 60) -(hours * 60));
int sekunden = (days * 86400) + (hours * 3600) +(minutes * 60);
NSString *timeDiff = [NSString stringWithFormat:#"%02d Tage : %02d Std : %02d Min",days, hours, minutes];
_showTimer.text = [NSString stringWithFormat:#"%02d",sekunden];
self.navigationItem.title = [NSString stringWithFormat:#"%#",timeDiff];
NSString *currentTimestamp = [NSString stringWithFormat:#"%.f", [resetTime timeIntervalSince1970]];
destinationDate = [NSDate dateWithTimeIntervalSince1970:1425981600];
UILocalNotification *localNotification = [[UILocalNotification alloc] init];
localNotification.fireDate =resetTime;
[[UIApplication sharedApplication] scheduleLocalNotification:localNotification];
NSLog(#"timer: %#",timeDiff);
NSLog(#"timer: %#",currentTimestamp);
}
and this for my Alert :
-(void)application:(UIApplication *)application didReceiveLocalNotification:(UILocalNotification *)notification
{
// code to display an alert here
SCLAlertView *alert = [[SCLAlertView alloc] init];
alert.backgroundType = Blur;
UIColor *color = [UIColor colorWithRed:52/255.0 green:222/255.0 blue:192/255.0 alpha:255/255.0];
[alert showCustom:self image:[UIImage imageNamed:#"geist"] color:color title:NSLocalizedString(#"Auto Reset", nil) subTitle:NSLocalizedString(#"Automatischer Reset Aktiviert!", nil) closeButtonTitle:#"OK" duration:0.0f];
}
when the time has expired and i start my App ... the Alert is not and it does not happen..
I need to do is activate the Notification somewhere?
thanks
Ok i have added your code now in the AppDelegate.m
-(void)application:(UIApplication *)application didReceiveLocalNotification:(UILocalNotification *)notification
{
UIAlertView* alert= [[UIAlertView alloc]initWithTitle:#"My App"
message:#"Reset"
delegate:nil
cancelButtonTitle:#"OK"
otherButtonTitles:nil];
[alert show];
}}
now it works, when i start my app the Alert shows, but it Loop Blinking and i can't Klick OK!
what is this now...?
This can be done by using NSLocalNotification
UILocalNotification *localNotification = [[UILocalNotification alloc] init];
localNotification.fireDate =<target date>;
[[UIApplication sharedApplication] scheduleLocalNotification:localNotification];
-(void)application:(UIApplication *)application didReceiveLocalNotification:(UILocalNotification *)notification
{
// code to display an alert here
}

Local notification won't stop sending until I opened the app

my app won't stop sending the notification I set for a specific time until I opened the app. It will send me like every 2 minutes or so to kind of remind me...
(void)applicationDidEnterBackground:(UIApplication *)application {
NSCalendar *calendar3 = [NSCalendar currentCalendar];
NSDateComponents *components3 = [calendar3 components:(NSYearCalendarUnit | NSMonthCalendarUnit | NSDayCalendarUnit | NSHourCalendarUnit | NSMinuteCalendarUnit) fromDate:[NSDate date]];
[components3 setHour:21];
[components3 setMinute:30];
UIApplication *app3 = [UIApplication sharedApplication];
UILocalNotification *notifyAlarm3 = [[UILocalNotification alloc] init];
if (notifyAlarm3) {
notifyAlarm3.fireDate = [calendar3 dateFromComponents:components3];
notifyAlarm3.timeZone = [NSTimeZone defaultTimeZone];
notifyAlarm3.repeatInterval = NSDayCalendarUnit;
notifyAlarm3.soundName = #"not.wav";
notifyAlarm3.alertBody = #"Hello";
[app3 scheduleLocalNotification:notifyAlarm3];
}
}
(void)applicationWillEnterForeground:(UIApplication *)application {
UIApplication *app = [UIApplication sharedApplication];
NSArray *oldNotifications = [app scheduledLocalNotifications];
if ([oldNotifications count] > 0) {
[app cancelAllLocalNotifications];
}
}
Looking forward to response!

UItextView dynamiclly Height and Link reconizer iOS 6 vs ios 7

I develop an application and I need to display the content using an UITextView which must have set the height dynamically and it must recognize a link.
I used code above:
self.textView.text = [NSString stringWithFormat:#"%# \n %#", self.offersObjects.body, self.offersObjects.url];
self.textView.dataDetectorTypes = UIDataDetectorTypeLink;
if (([[[UIDevice currentDevice] systemVersion] integerValue] < 7)){
CGRect frame = self.textView.frame;
frame.size.height = self.textView.contentSize.height;contentSize.height;
self.textView.frame = frame;
}else{
[self.textView sizeToFit];
[self.textView layoutIfNeeded];
}
My problem is that it doesn't recognize the link .
try with below code :
-(IBAction)txtStustes:(id)sender
{
NSError *error = nil;
NSDataDetector *detector = [NSDataDetector dataDetectorWithTypes:NSTextCheckingTypeLink
| NSTextCheckingTypePhoneNumber error:&error];
NSString *string = self.textView.text;
NSArray *matches = [detector matchesInString:string options:0 range:NSMakeRange(0, [string length])];
for (NSTextCheckingResult *match in matches) {
if ([match resultType] == NSTextCheckingTypeLink) {
NSURL *url = [match URL];
[[UIApplication sharedApplication] openURL:url];
}
}
}
Also add below code in your viewDidLoad method
UITapGestureRecognizer *LblProfileNameTouch=[[UITapGestureRecognizer alloc]initWithTarget:self action:#selector(txtStustes:)];
[LblProfileNameTouch setNumberOfTouchesRequired:1];
[self.textView addGestureRecognizer:LblProfileNameTouch];

How to add some more marks on date tiles after loading calendar in tapku?

I am using TAPKU calendar in my iOS application. I want to add some more marks on date tile after loading complete data in tapku calendar.
I am getting some additional data from async process and I want to mark that data also on calendar.
How can i do this. Thanks in advance.
This is indeed possible, as an example for the day calendar view, modify _refreshDataPageWithAtIndex to be like this:
- (void) _refreshDataWithPageAtIndex:(NSInteger)index{
UIScrollView *sv = self.pages[index];
TKTimelineView *timeline = [self _timelineAtIndex:index];
CGRect r = CGRectInset(self.horizontalScrollView.bounds, HORIZONTAL_PAD, 0);
r.origin.x = self.horizontalScrollView.frame.size.width * index + HORIZONTAL_PAD;
sv.frame = r;
timeline.startY = VERTICAL_INSET;
for (UIView* view in sv.subviews) {
if ([view isKindOfClass:[TKCalendarDayEventView class]]){
[self.eventGraveYard addObject:view];
[view removeFromSuperview];
}
}
if(self.nowLineView.superview == sv) [self.nowLineView removeFromSuperview];
if([timeline.date isTodayWithTimeZone:self.timeZone]){
NSDate *date = [NSDate date];
NSDateComponents *comp = [date dateComponentsWithTimeZone:self.timeZone];
NSInteger hourStart = comp.hour;
CGFloat hourStartPosition = hourStart * VERTICAL_DIFF + VERTICAL_INSET;
NSInteger minuteStart = round(comp.minute / 5.0) * 5;
CGFloat minuteStartPosition = roundf((CGFloat)minuteStart / 60.0f * VERTICAL_DIFF);
CGRect eventFrame = CGRectMake(self.nowLineView.frame.origin.x, hourStartPosition + minuteStartPosition - 5, NOB_SIZE + self.frame.size.width - LEFT_INSET, NOB_SIZE);
self.nowLineView.frame = eventFrame;
[sv addSubview:self.nowLineView];
}
if(!self.dataSource) return;
timeline.events = [NSMutableArray new];
[self.dataSource calendarDayTimelineView:self eventsForDate:timeline.date andEvents:timeline.events success:^{
[timeline.events sortUsingComparator:^NSComparisonResult(TKCalendarDayEventView *obj1, TKCalendarDayEventView *obj2){
return [obj1.startDate compare:obj2.startDate];
}];
[self _realignEventsAtIndex:index];
if(self.nowLineView.superview == sv)
[sv bringSubviewToFront:self.nowLineView];
}];
}
and then change your eventsForDate function to look like this:
- (void) calendarDayTimelineView:(TKCalendarDayView*)calendarDayTimeline eventsForDate:(NSDate *)eventDate andEvents:(NSMutableArray *)events success:(void (^)())success {
[Model doSomethingAsync andSuccess:^(NSArray *classes) {
// .. Add stuff to events..
success();
}];
}
I'm assuming the pattern for the other controls is very similar. The premise is you're waiting to continue the formatting/layout flow til you get your data.

Use calendarItemWithIdentifier to lookup calendar event

I'm trying to lookup a calendar event by the new iOS method calendarItemWithIdentifier. I can't use the eventWithIdentifier because the identifier is changed after the event is syncronized with the server. The calendarItemIdentifier is not.
But the calendarItemWithIdentifier always returns (null).
EKEventStore *store = [[EKEventStore alloc] init];
[store requestAccessToEntityType:EKEntityTypeEvent completion:^(BOOL granted, NSError *error) {
if (granted) {
// Create event.
EKEvent *event = [EKEvent eventWithEventStore:store];
event.title = self.title;
event.startDate = [[NSDate date] dateByAddingTimeInterval:3600];
event.endDate = [[NSDate date] dateByAddingTimeInterval:7200];
event.timeZone = [NSTimeZone defaultTimeZone];
event.calendar = [store defaultCalendarForNewEvents];
BOOL success = [store saveEvent:event span:EKSpanThisEvent commit:YES error:&error];
if (success)
{
NSString *calendarItemIdentifier = event.calendarItemIdentifier;
NSLog(#"Assigned identifier: %#", calendarItemIdentifier);
// Look up the event in the calendar.
event = (EKEvent *)[store calendarItemWithIdentifier:calendarItemIdentifier];
if (event) {
NSLog(#"FOUND");
} else {
NSLog(#"NOT FOUND");
}
}
}
}];
From the log:
2013-01-13 10:32:52.042 CalendarIntegration[6095:1303] Assigned identifier: C5FD3792-EBF1-4766-B27D-2767E5C8F3BE
2013-01-13 10:32:52.043 CalendarIntegration[6095:1303] NOT FOUND
Help would be appreciated.
According the doc, this behavior is as expected, link,
A full sync with the calendar will lose this identifier. You should have a plan for dealing with a calendar whose identifier is no longer fetch-able by caching its other properties.

Resources