UIDocumentInteractionController日历访问 [英] UIDocumentInteractionController Calendar Access

查看:87
本文介绍了UIDocumentInteractionController日历访问的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个ics(日历)文件,我用 UIDocumentInteractionController 打开,使用 presentOptionsMenuFromRect:。运行时,打开方式菜单看起来像这个

I have an ics (Calendar) file that I'm opening with a UIDocumentInteractionController, using presentOptionsMenuFromRect:. When this runs, the "Open In" menu looks like this.

如您所见,没有添加到日历选项。这就是我得到的:我使用与.vcf(联系人卡片)文件完全相同的代码,并且按预期工作,可以使用打开联系人选项。

As you can see, no "Add to Calendar" option. Here's what gets me: I'm using the same exact code for a .vcf (contact card) file, and it works as expected with the "Open In Contacts" option available.

我在 Info.plist 中是否遗漏了某些用于日历访问的权限?为什么 UIDocumentInteractionController 无法正确处理 .ics 文件类型,但 .vcf 工作正常吗?这两种文件类型非常相似。从选项菜单中,如果我将ics文件邮寄给我自己并从邮件应用程序打开它,它就可以正常读取,我可以将事件添加到我的日历中,因此我知道数据是有效的。我搜索了高低的解决方案,似乎没有人知道为什么日历访问不起作用。我遇到的一些问题仍然没有答案:

Am I missing some sort of permission in my Info.plist for calendar access? Why can't UIDocumentInteractionController handle the .ics file type correctly, but .vcf works just fine? Those two file types are very similar. From the options menu, if I mail the ics file to myself and open it from the mail app, it reads it just fine, and I can add the events to my calendar, so I know the data is valid. I've searched high and low for a solution, and nobody seems to know why Calendar access isn't working. Some questions I've come across that remain unanswered:

无法将ics文件添加到日历

如何让UIDocumentInteractionController显示Calendar作为打开.ics文件的选项?

如果Apple故意这样做,我能想到的唯一原因是因为他们宁愿开发人员使用EventKit向Calendar添加事件。如果是真的,那解决方案就相当令人沮丧。任何关于这个问题的见解都将非常感激。

If Apple is doing this deliberately, the only reason I can think of is because they'd rather developers use EventKit to add events to Calendar. If true, that solution is rather frustrating. Any insight on this problem would be much appreciated.

推荐答案

我最终通过( https://github.com/KiranPanesar/MXLCalendarManager )。然后,我可以使用EventKit将下载的.ics文件解析为EKEvent并通过EKEventEditViewController打开它( https://developer.apple.com/library/prerelease/ios/samplecode/SimpleEKDemo/Listings/Classes_RootViewController_m.html )。有点圆,但似乎工作。以下是我设置实现此功能的webview控制器类的方法:

I ended up downloading the .ics file via (https://github.com/KiranPanesar/MXLCalendarManager). I was then able to use EventKit to parse the downloaded .ics file into an EKEvent and open it via EKEventEditViewController (https://developer.apple.com/library/prerelease/ios/samplecode/SimpleEKDemo/Listings/Classes_RootViewController_m.html). A little round about but seemed to work. Here's how I setup my webview controller class that implements this:

@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。我准备好了我的特定类型的.ics格式。例如,我的.ics文件的摘要部分如下所示:

I also had to slightly modify parts of MXLCalendarManager.m to be ready for my specific types of .ics formatting. For example, my summary section of my .ics file looks like:

DESCRIPTION;LANGUAGE=en-us:The following details your appointment:\n\n\n

其中MXLCalendarManager仅查找:

where as MXLCalendarManager is only looking for:

DESCRIPTION: (Something). 

我修改了代码以便考虑; ln。这也删除了所有人工换行符,但允许我在摘要说明中添加我自己的换行符:

I modified the code as such to account for the ;ln. This also removed all the artificial line breaks but allowed me add my own in the summary description:

    // 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"];
    }

这篇关于UIDocumentInteractionController日历访问的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆