ios - UIDocumentInteractionController 日历访问

标签 ios cocoa-touch nsdata icalendar uidocumentinteraction

我有一个 ics(日历)文件,我使用 presentOptionsMenuFromRect: 使用 UIDocumentInteractionController 打开它。运行时,“打开方式”菜单看起来像 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。如果为真,则该解决方案相当令人沮丧。对此问题的任何见解将不胜感激。

最佳答案

我最终通过 ( 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 Controller 类:

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

关于ios - UIDocumentInteractionController 日历访问,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27927665/

相关文章:

ios - Swift Core Data NSPredicate 静态字符串问题

ios - UITextField attributedPlaceholder 颜色和不透明度在 swift 4

ios - 致命异常 NSRangeException -[__NSCFString replaceOccurrencesOfString :withString:options:range:]: Range {N, N} 越界;字符串长度 N

ios - 使用自动布局时设置 UICollectionViewFlowLayout 的属性不起作用

iphone - NSData 到 NSString 的转换问题!

ios - 在 iOS 中,如何从子 ViewController 获取图像?

ios - 从另一个应用程序打开 Safari 的深层链接

ios - 禁用 MFMessageComposeViewController 的自定义外观

swift - Swift Playground 中的数据值变化

ios - 如何在 IB 的 tableView 的第一个单元格上方添加一个 UI 元素?