简体   繁体   中英

iOS - Add custom URI of our app to the event stored in calendar programmatically

i have a scenario where i am adding the event in calendar from my application. i have a custom url scheme for my app as myapp://

the code i am using to store the event in calendar is

-(void)addEventToCalender
{
NSDateComponents *components = [[NSDateComponents alloc]init];
[components setYear:2014];
[components setMonth:6];
[components setDay:15];
[components setHour:10];
[components setMinute:15];

NSCalendar *cal = [[NSCalendar alloc] initWithCalendarIdentifier:NSGregorianCalendar];
NSDate *eventDateAndTime = [cal dateFromComponents:components];
NSLog(@"Event Date and Time : %@",eventDateAndTime);

EKEventStore *store = [[EKEventStore alloc]init];

EKEvent *event = [EKEvent eventWithEventStore:store];

event.title = @"ttrrpp Trip";
event.notes = @"Go to application myapp://";
event.startDate = eventDateAndTime;
event.endDate = [[NSDate alloc]initWithTimeInterval:600 sinceDate:eventDateAndTime];

[store requestAccessToEntityType:EKEntityTypeEvent completion:^(BOOL granted, NSError *error)
 {
     if (granted)
     {
         [event setCalendar:[store defaultCalendarForNewEvents]];
         NSError *err = nil;
         [store saveEvent:event span:EKSpanThisEvent commit:YES error:&err];
         NSString *savedEventId = event.eventIdentifier;
         NSLog(@"Saved Event ID : %@",savedEventId);

     }
     else
     {
         UIAlertView *alert = [[UIAlertView alloc]initWithTitle:@"ttrrpp" message:@"You have denied to provide access to calender. No events will be added." delegate:self cancelButtonTitle:@"OK" otherButtonTitles:nil];
         [alert show];
     }
 }];

}

The event is added in calendar but i want to pass the url of my app to the notes so that it opens my app when the link in event notes is clicked. Kindly help me. Thanks in advance.

You are very close ;-) . Assuming you have defined your custom URL scheme in your .plist, add a 'host' and 'path' to your custom URL scheme being placed in event.notes. (eg. myapp://myhost/mypath) Then something like...

-(BOOL)application:(UIApplication*)application handleOpenURL:(NSURL*)url { - deprecated
-(BOOL) application:(UIApplication *)application openURL:(NSURL *)url sourceApplication:(NSString *)sourceApplication annotation:(id)annotation {

    if (!url) {
        return NO;
    }

    if([[url host] isEqualToString:@"myhost"])
    {
        //can also check [url path] if you want to
        //do whatever
    }

    return YES;
}

PS: deprecated method worked for me with iOS6/7, haven't tried new method.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM