[英]How to delete event from eventkit
This is the code in my detail view it can delete event in when u come to detail view press the switch to add event and then press it again to delete it. 这是我的详细信息视图中的代码,当您进入详细信息视图时,它可以删除事件,请按开关以添加事件,然后再次按它以将其删除。 However, when u come to detail view and then press back (using navigation) and then come o the same page again it cannot delete the event please help 但是,当您进入详细视图然后按回退(使用导航),然后再次进入同一页面时,它无法删除该事件,请帮助
there are more function but I delete just focus on Switch and EVENTKIT 还有更多功能,但我删除只是关注Switch和EVENTKIT
//
// DetailScheduleViewController.m
// Register
//
// Created by junejubu on 3/10/2558 BE.
// Copyright (c) 2558 Thananont Aunsiripant. All rights reserved.
//
- (void)viewDidLoad {
[super viewDidLoad];
//NSLog(@"check show id sch ::%@",self.Show_ID);
self.Switch.on = NO;
self.lblNotification.text = @"Notification OFF";
[self getUserNotification];
[self getUserFollow];
}
- (void)didReceiveMemoryWarning
{
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
}
- (void)getUserNotification
{
NSString *urlStr2 = @"http://ptvshowthai.com/getNotification.php";
NSURL *url2 = [NSURL URLWithString:urlStr2];
self.getnoti = [ASIFormDataRequest requestWithURL:url2];
self.getnoti.requestMethod = @"POST"; //DELETE, PUT
self.getnoti.delegate = self;
self.getnoti.timeOutSeconds = 30;
[self.getnoti setPostValue: self.FK_User_ID forKey:@"sMemberID"];
[self.getnoti setValidatesSecureCertificate:NO];
[self.getnoti startAsynchronous];
}
- (void)requestFinished:(ASIHTTPRequest *)request
{
NSLog(@"requestFinished");
if (request == self.getnoti) {
self.dictnoti = [NSJSONSerialization JSONObjectWithData:[request responseData] options:NSJSONReadingAllowFragments error:nil];
self.NOTI = [[NSMutableArray alloc] init];
for (NSDictionary *dataDic in self.dictnoti )
{
// unsigned long count2 = [self.dict2 count];
NSString *ShowID2 = [dataDic objectForKey:@"Show_ID"];
self.str2 = [dataDic objectForKey:@"Event_Store"];
// NSLog(@"check str2 :: %@",self.str2);
[self.NOTI addObject:ShowID2];
}
NSLog(@"Show who noti ::%@",self.NOTI);
// NSLog(@"check str2 :: %@",self.str2);
}
if(request ==self.getFollow){
self.dict2 = [NSJSONSerialization JSONObjectWithData:[request responseData] options:NSJSONReadingAllowFragments error:nil];
NSLog(@"Dict getFollow :: %@",self.dict2);
//unsigned long count2 = [self.dict2 count];
//NSLog(@"show count ::%lu",count2);
_Show = [[NSMutableArray alloc] init];
// NSDictionary *dict;
for (NSDictionary *dataDic in self.dict2 )
{
// unsigned long count2 = [self.dict2 count];
NSString *ShowID = [dataDic objectForKey:@"Show_ID"];
[self.Show addObject:ShowID];
}
if ([self.Show containsObject: self.Show_ID]) {
_lblFollow.text = @"Following";
_lblFollow.textColor = [UIColor greenColor];
_i = @"0";
NSLog(@"check i green :: %@",_i);
self.Switch.hidden = NO;
NSLog(@"check self.noti : %@",self.NOTI);
if ([self.NOTI containsObject: self.Show_ID]){
self.Switch.on = YES;
}
self.lblNotification.hidden = NO;
[self.Switch addTarget:self action:@selector(Onoff) forControlEvents:UIControlEventValueChanged];
}
else{
_lblFollow.text = @"Follow";
_lblFollow.textColor = [UIColor redColor];
_i = @"1";
NSLog(@"check i red :: %@",_i);
self.Switch.hidden = YES;
self.Switch.on = NO;
self.lblNotification.hidden = YES;
[self.Switch addTarget:self action:@selector(Onoff) forControlEvents:UIControlEventValueChanged];
}
}
}
-(void)addevent{
_eventStore = [[EKEventStore alloc] init];
if ([_eventStore respondsToSelector:@selector(requestAccessToEntityType:completion:)])
{
[_eventStore requestAccessToEntityType:EKEntityTypeEvent completion:^(BOOL granted, NSError *error) {
if (granted){
addEventgranted = 1;
EKEvent *event = [EKEvent eventWithEventStore:_eventStore];
[event setTitle:self.showNameTitle];
[event setStartDate: self.today];
[event setEndDate:[[NSDate alloc]initWithTimeInterval:self.duration sinceDate:event.startDate]];
NSTimeInterval alarmOffset = -300;
EKAlarm *alarm = [EKAlarm alarmWithRelativeOffset:alarmOffset];
[event addAlarm:alarm];
[event setCalendar:[_eventStore defaultCalendarForNewEvents]];
NSError *err;
[_eventStore saveEvent:event span:EKSpanThisEvent error:&err];
self.str = [[NSString alloc] initWithFormat:@"%@", event.eventIdentifier];
NSLog(@"check self.str :: %@",self.str);
NSString *url = @"http://ptvshowthai.com/insertNotification.php";
NSURL *urls = [NSURL URLWithString:url];
self.formData = [ASIFormDataRequest requestWithURL:urls];
self.formData.requestMethod = @"POST";
self.formData.delegate = self;
self.formData.timeOutSeconds = 30;
[self.formData setPostValue: self.FK_User_ID forKey:@"sMemberID"];
[self.formData setPostValue: self.Show_ID forKey:@"showID"];
NSLog(@"check str::: %@",self.str);
[self.formData setPostValue: self.str forKey:@"event"];
[self.formData setValidatesSecureCertificate:NO];
[self.formData startAsynchronous];
_lblFollow.text = @"Following";
_lblFollow.textColor = [UIColor greenColor];
NSLog(@"Follow");
[self getUserNotification];
}
}];
}
}
-(void)alert
{
if (addEventgranted == 1) {
UIAlertView *alert = [[UIAlertView alloc]initWithTitle:@"Success" message:@"Event Successfully added" delegate:self cancelButtonTitle:@"OK" otherButtonTitles: nil];
[alert show];
}
}
-(void)Onoff
{
if (self.Switch.on) {
[self addevent];
[self performSelector:@selector(alert) withObject:nil afterDelay:0.3];
self.lblNotification.text =@"Notification On";
}
else
{
NSLog(@"check str22::%@",self.str2);
EKEvent* event2 = [_eventStore eventWithIdentifier:self.str2];
if (event2 != nil) {
NSError* error = nil;
[_eventStore removeEvent:event2 span:EKSpanThisEvent error:&error];
}
self.lblNotification.text =@"Notification OFF";
NSString *url = @"http://ptvshowthai.com/deleteNotification.php";
NSURL *urls = [NSURL URLWithString:url];
self.formData = [ASIFormDataRequest requestWithURL:urls];
self.formData.requestMethod = @"POST";
self.formData.delegate = self;
self.formData.timeOutSeconds = 30;
[self.formData setPostValue: self.FK_User_ID forKey:@"sMemberID"];
[self.formData setPostValue: self.Show_ID forKey:@"showID"];
[self.formData setValidatesSecureCertificate:NO];
[self.formData startAsynchronous];
_lblFollow.text = @"Following";
_lblFollow.textColor = [UIColor greenColor];
NSLog(@"Follow");
[self getUserNotification];
}
}
Add below line of code at switch off condition also 在关闭状态下也添加下面的代码行
Its better to add below line at ViewDidLoad instead of addEvent method.. 最好在ViewDidLoad的下面添加一行而不是addEvent方法。
_eventStore = [[EKEventStore alloc] init];
Hope it fixes..! 希望它能解决..!
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.