Attach iCal File in MFMailComposerController?

2019-06-12 04:37发布

问题:

I want to add Event in my iPhone Calendar, i Successfully add the Event in my iPhone calendar. But, i want to get the all Current Month event and i want to attach that Event file (.ical) in my MFMailComposer.

回答1:

Reading events is very simple.

// Create the predicate from the event store's instance method
NSPredicate *predicate = [store predicateForEventsWithStartDate:startOfTheMonth
                                                        endDate:endOfTheMonth
                                                      calendars:nil];
// Fetch all events that match the predicate
NSArray *events = [store eventsMatchingPredicate:predicate];

More information in apple docs.

To get start and end of the month you can use example from this project: https://github.com/melsam/NSDateCategoryForReporting

And use this as an example how to export Events to .ical file https://github.com/mysterioustrousers/EKEventToiCal/blob/master/EKEventToiCal/

To send .ical file use the code from IronManGill answer but change mimeType to text/calendar

[picker addAttachmentData:data mimeType:@"text/calendar" fileName:@"/abc.ical"];


回答2:

Well I can offer you a workaround. If you get the .ical file , have access to it. You could convert it into a .zip file , please go through these links :-

How can I create a zip file by using Objective C?

How to zip folders in iPhone SDK?

Creating zip files in ObjectiveC for iPhone

And then attach it alongwith the email in the MFMailComposer using this

MFMailComposeViewController *picker = [[MFMailComposeViewController alloc] init]; picker.mailComposeDelegate = self; NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);

NSString *documentsDirectory = [paths objectAtIndex:0];    
NSString *WritableDBPath= [documentsDirectory stringByAppendingPathComponent:kFilename];    
NSData *data = [NSData dataWithContentsOfFile:WritableDBPath];    
[picker addAttachmentData:data mimeType:@"application/zip" fileName:@"/abc.zip"];
[picker setSubject:@"Database"];    
[picker setMessageBody:@"Database testing" isHTML:NO];    
[self presentModalViewController:picker animated:YES];

Hope this helps.