mirror of
https://github.com/status-im/react-native-mail.git
synced 2026-08-31 03:31:13 +00:00
193 lines
7.1 KiB
Objective-C
193 lines
7.1 KiB
Objective-C
#import <MessageUI/MessageUI.h>
|
|
#import "RNMail.h"
|
|
#import <React/RCTConvert.h>
|
|
#import <React/RCTLog.h>
|
|
|
|
@implementation RNMail
|
|
{
|
|
NSMutableDictionary *_callbacks;
|
|
}
|
|
|
|
- (instancetype)init
|
|
{
|
|
if ((self = [super init])) {
|
|
_callbacks = [[NSMutableDictionary alloc] init];
|
|
}
|
|
return self;
|
|
}
|
|
|
|
- (dispatch_queue_t)methodQueue
|
|
{
|
|
return dispatch_get_main_queue();
|
|
}
|
|
|
|
+ (BOOL)requiresMainQueueSetup
|
|
{
|
|
return YES;
|
|
}
|
|
|
|
RCT_EXPORT_MODULE()
|
|
|
|
RCT_EXPORT_METHOD(mail:(NSDictionary *)options
|
|
callback: (RCTResponseSenderBlock)callback)
|
|
{
|
|
if ([MFMailComposeViewController canSendMail])
|
|
{
|
|
MFMailComposeViewController *mail = [[MFMailComposeViewController alloc] init];
|
|
mail.mailComposeDelegate = self;
|
|
_callbacks[RCTKeyForInstance(mail)] = callback;
|
|
|
|
if (options[@"subject"]){
|
|
NSString *subject = [RCTConvert NSString:options[@"subject"]];
|
|
[mail setSubject:subject];
|
|
}
|
|
|
|
bool *isHTML = NO;
|
|
|
|
if (options[@"isHTML"]){
|
|
isHTML = [options[@"isHTML"] boolValue];
|
|
}
|
|
|
|
if (options[@"body"]){
|
|
NSString *body = [RCTConvert NSString:options[@"body"]];
|
|
[mail setMessageBody:body isHTML:isHTML];
|
|
}
|
|
|
|
if (options[@"recipients"]){
|
|
NSArray *recipients = [RCTConvert NSArray:options[@"recipients"]];
|
|
[mail setToRecipients:recipients];
|
|
}
|
|
|
|
if (options[@"ccRecipients"]){
|
|
NSArray *ccRecipients = [RCTConvert NSArray:options[@"ccRecipients"]];
|
|
[mail setCcRecipients:ccRecipients];
|
|
}
|
|
|
|
if (options[@"bccRecipients"]){
|
|
NSArray *bccRecipients = [RCTConvert NSArray:options[@"bccRecipients"]];
|
|
[mail setBccRecipients:bccRecipients];
|
|
}
|
|
|
|
if (options[@"attachment"] && options[@"attachment"][@"path"] && options[@"attachment"][@"type"]){
|
|
NSString *attachmentPath = [RCTConvert NSString:options[@"attachment"][@"path"]];
|
|
NSString *attachmentType = [RCTConvert NSString:options[@"attachment"][@"type"]];
|
|
NSString *attachmentName = [RCTConvert NSString:options[@"attachment"][@"name"]];
|
|
|
|
// Set default filename if not specificed
|
|
if (!attachmentName) {
|
|
attachmentName = [[attachmentPath lastPathComponent] stringByDeletingPathExtension];
|
|
}
|
|
|
|
// Get the resource path and read the file using NSData
|
|
NSData *fileData = [NSData dataWithContentsOfFile:attachmentPath];
|
|
|
|
// Determine the MIME type
|
|
NSString *mimeType;
|
|
|
|
/*
|
|
* Add additional mime types and PR if necessary. Find the list
|
|
* of supported formats at http://www.iana.org/assignments/media-types/media-types.xhtml
|
|
*/
|
|
if ([attachmentType isEqualToString:@"jpg"]) {
|
|
mimeType = @"image/jpeg";
|
|
} else if ([attachmentType isEqualToString:@"png"]) {
|
|
mimeType = @"image/png";
|
|
} else if ([attachmentType isEqualToString:@"doc"]) {
|
|
mimeType = @"application/msword";
|
|
} else if ([attachmentType isEqualToString:@"docx"]) {
|
|
mimeType = @"application/vnd.openxmlformats-officedocument.wordprocessingml.document";
|
|
} else if ([attachmentType isEqualToString:@"ppt"]) {
|
|
mimeType = @"application/vnd.ms-powerpoint";
|
|
} else if ([attachmentType isEqualToString:@"pptx"]) {
|
|
mimeType = @"application/vnd.openxmlformats-officedocument.presentationml.presentation";
|
|
} else if ([attachmentType isEqualToString:@"html"]) {
|
|
mimeType = @"text/html";
|
|
} else if ([attachmentType isEqualToString:@"csv"]) {
|
|
mimeType = @"text/csv";
|
|
} else if ([attachmentType isEqualToString:@"pdf"]) {
|
|
mimeType = @"application/pdf";
|
|
} else if ([attachmentType isEqualToString:@"vcard"]) {
|
|
mimeType = @"text/vcard";
|
|
} else if ([attachmentType isEqualToString:@"json"]) {
|
|
mimeType = @"application/json";
|
|
} else if ([attachmentType isEqualToString:@"zip"]) {
|
|
mimeType = @"application/zip";
|
|
} else if ([attachmentType isEqualToString:@"text"]) {
|
|
mimeType = @"text/*";
|
|
} else if ([attachmentType isEqualToString:@"mp3"]) {
|
|
mimeType = @"audio/mpeg";
|
|
} else if ([attachmentType isEqualToString:@"wav"]) {
|
|
mimeType = @"audio/wav";
|
|
} else if ([attachmentType isEqualToString:@"aiff"]) {
|
|
mimeType = @"audio/aiff";
|
|
} else if ([attachmentType isEqualToString:@"flac"]) {
|
|
mimeType = @"audio/flac";
|
|
} else if ([attachmentType isEqualToString:@"ogg"]) {
|
|
mimeType = @"audio/ogg";
|
|
} else if ([attachmentType isEqualToString:@"xls"]) {
|
|
mimeType = @"application/vnd.ms-excel";
|
|
} else if ([attachmentType isEqualToString:@"ics"]) {
|
|
mimeType = @"text/calendar";
|
|
} else if ([attachmentType isEqualToString:@"xlsx"]) {
|
|
mimeType = @"application/vnd.openxmlformats-officedocument.spreadsheetml.sheet";
|
|
}
|
|
|
|
// Add attachment
|
|
[mail addAttachmentData:fileData mimeType:mimeType fileName:attachmentName];
|
|
}
|
|
|
|
UIViewController *root = [[[[UIApplication sharedApplication] delegate] window] rootViewController];
|
|
|
|
while (root.presentedViewController) {
|
|
root = root.presentedViewController;
|
|
}
|
|
[root presentViewController:mail animated:YES completion:nil];
|
|
} else {
|
|
callback(@[@"not_available"]);
|
|
}
|
|
}
|
|
|
|
#pragma mark MFMailComposeViewControllerDelegate Methods
|
|
|
|
- (void)mailComposeController:(MFMailComposeViewController *)controller didFinishWithResult:(MFMailComposeResult)result error:(NSError *)error
|
|
{
|
|
NSString *key = RCTKeyForInstance(controller);
|
|
RCTResponseSenderBlock callback = _callbacks[key];
|
|
if (callback) {
|
|
switch (result) {
|
|
case MFMailComposeResultSent:
|
|
callback(@[[NSNull null] , @"sent"]);
|
|
break;
|
|
case MFMailComposeResultSaved:
|
|
callback(@[[NSNull null] , @"saved"]);
|
|
break;
|
|
case MFMailComposeResultCancelled:
|
|
callback(@[[NSNull null] , @"cancelled"]);
|
|
break;
|
|
case MFMailComposeResultFailed:
|
|
callback(@[@"failed"]);
|
|
break;
|
|
default:
|
|
callback(@[@"error"]);
|
|
break;
|
|
}
|
|
[_callbacks removeObjectForKey:key];
|
|
} else {
|
|
RCTLogWarn(@"No callback registered for mail: %@", controller.title);
|
|
}
|
|
UIViewController *ctrl = [[[[UIApplication sharedApplication] delegate] window] rootViewController];
|
|
while (ctrl.presentedViewController && ctrl != controller) {
|
|
ctrl = ctrl.presentedViewController;
|
|
}
|
|
[ctrl dismissViewControllerAnimated:YES completion:nil];
|
|
}
|
|
|
|
#pragma mark Private
|
|
|
|
static NSString *RCTKeyForInstance(id instance)
|
|
{
|
|
return [NSString stringWithFormat:@"%p", instance];
|
|
}
|
|
|
|
@end
|