gpt4 book ai didi

ios - UIDocumentInteractionController 日历访问

转载 作者:行者123 更新时间:2023-11-29 10:34:15 26 4
gpt4 key购买 nike

我有一个 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/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com