iOS - 以编程方式将日历应用的自定义URI添加到日历中存储的事件中

时间:2014-06-12 07:44:32

标签: ios iphone objective-c ios7

我有一个场景,我在我的应用程序中添加日历中的事件。我的应用程序的自定义网址方案为myapp://

我用于在日历中存储事件的代码是

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

}

该事件已添加到日历中,但我想将我的应用的网址传递给笔记,以便在点击事件备注中的链接时打开我的应用。请帮助我。 提前谢谢。

1 个答案:

答案 0 :(得分:3)

你很亲密;-)。假设您已在.plist中定义了自定义网址方案,请添加“主机”#39;和'路径'您的自定义URL方案放在event.notes中。 (例如,myapp:// myhost / mypath)然后就像......

-(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:不推荐的方法适用于iOS6 / 7,没有尝试过新方法。