我有一个ics(日历)文件,我使用UIDocumentInteractionController
打开presentOptionsMenuFromRect:
。当这个运行时,"打开"菜单看起来像this。
正如您所见,没有"添加到日历"选项。以下是我的原因:我使用相同的.vcf(联系人卡片)文件的相同代码,works as expected使用"打开联系人"选项可用。
我在Info.plist
日历访问中错过了某种权限吗?为什么UIDocumentInteractionController
无法正确处理.ics
文件类型,但.vcf
工作正常?这两种文件类型非常相似。从选项菜单中,如果我将ics文件发送给自己并从邮件应用程序打开它,它就可以正常读取,我可以将事件添加到我的日历中,因此我知道数据是有效的。我已经搜索了一个解决方案的高低,似乎没有人知道为什么日历访问不起作用。我遇到的一些问题仍然没有答案:
Unable to Add ics file to Calendar
How can I have UIDocumentInteractionController show Calendar as an option for opening a .ics file?
如果Apple故意这样做,我能想到的唯一原因是因为他们宁愿开发人员使用EventKit向Calendar添加事件。如果是真的,那解决方案就相当令人沮丧。任何关于这个问题的见解都将非常感激。
答案 0 :(得分:2)
我最终通过(https://github.com/KiranPanesar/MXLCalendarManager)下载了.ics文件。然后,我可以使用EventKit将下载的.ics文件解析为EKEvent,并通过EKEventEditViewController(https://developer.apple.com/library/prerelease/ios/samplecode/SimpleEKDemo/Listings/Classes_RootViewController_m.html)打开它。有点圆,但似乎工作。以下是我如何设置实现此功能的webview控制器类:
@interface WebViewController : UIViewController <UIWebViewDelegate, EKEventEditViewDelegate> {
// EKEventStore instance associated with the current Calendar application
@property (nonatomic, strong) EKEventStore *eventStore;
// Default calendar associated with the above event store
@property (nonatomic, strong) EKCalendar *defaultCalendar;
@end
@implementation WebViewController
- (void)viewDidLoad {
[super viewDidLoad];
...
// Initialize the event store
self.eventStore = [[EKEventStore alloc] init];
}
- (BOOL)webView:(UIWebView *)webView shouldStartLoadWithRequest:(NSURLRequest *)request navigationType:(UIWebViewNavigationType)navigationType {
NSURL *url = [request URL];
NSString *path = [url absoluteString];
NSRange range = [path rangeOfString:@".ics" options:NSCaseInsensitiveSearch];
if (range.length > 0) {
[self checkCalendarAndAddEvent:url];
return NO;
}
[UIApplication sharedApplication].networkActivityIndicatorVisible = YES;
return YES;
}
-(void)checkCalendarAndAddEvent:(NSURL*)url
{
EKAuthorizationStatus status = [EKEventStore authorizationStatusForEntityType:EKEntityTypeEvent];
if(status == EKAuthorizationStatusAuthorized)
{
[self addEventToCalendar:url];
} else
{
[self.eventStore requestAccessToEntityType:EKEntityTypeEvent completion:^(BOOL granted, NSError *error)
{
if (granted)
{
// Let's ensure that our code will be executed from the main queue
dispatch_async(dispatch_get_main_queue(), ^{
// The user has granted access to their Calendar; add to calendar
[self addEventToCalendar:url];
});
}else
{
UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"Privacy Warning" message:@"Permission was not granted for Calendar"
delegate:nil
cancelButtonTitle:@"OK"
otherButtonTitles:nil];
[alert show];
}
}];
}
}
-(void) addEventToCalendar: (NSURL *)url
{
MXLCalendarManager* calendarManager = [[MXLCalendarManager alloc] init];
self.defaultCalendar = self.eventStore.defaultCalendarForNewEvents;
[calendarManager scanICSFileAtRemoteURL:url withCompletionHandler:^(MXLCalendar *calendar, NSError *error) {
MXLCalendarEvent *mxlEvent = calendar.events.firstObject;
EKEventEditViewController *addController = [[EKEventEditViewController alloc] init];
EKEvent * event = [EKEvent eventWithEventStore:self.eventStore];
event.location = mxlEvent.eventLocation;
event.startDate = mxlEvent.eventStartDate;
event.endDate = mxlEvent.eventEndDate;
event.title = mxlEvent.eventSummary;
event.notes = mxlEvent.eventDescription;
addController.event = event;
// Set addController's event store to the current event store
addController.eventStore = self.eventStore;
addController.editViewDelegate = self;
[self presentViewController:addController animated:YES completion:nil];
}];
}
@end
我还必须略微修改部分MXLCalendarManager.m,以便为我特定类型的.ics格式做好准备。例如,我的.ics文件的摘要部分如下所示:
DESCRIPTION;LANGUAGE=en-us:The following details your appointment:\n\n\n
其中MXLCalendarManager仅查找:
DESCRIPTION: (Something).
我修改了代码,以便考虑到; ln。这也删除了所有人工换行符,但允许我在摘要说明中添加我自己的换行符:
// Extract event description
[eventScanner scanUpToString:@"DESCRIPTION" intoString:nil];
[eventScanner scanUpToString:@":" intoString:nil];
[eventScanner scanUpToString:@"\nSEQUENCE" intoString:&descriptionString];
if(descriptionString.length > 1)
{
descriptionString = [descriptionString substringFromIndex:1];
descriptionString = [[[descriptionString stringByReplacingOccurrencesOfString:@"\nSEQUENCE" withString:@""] stringByReplacingOccurrencesOfString:@"\r\n " withString:@""] stringByReplacingOccurrencesOfString:@"\\n" withString:@"\n"];
}