- Home /
Plugin iOS to send Mail with MFMailComposeViewController
Hello everyone,
First excuse me for my bad english. I would like to create a plugin for iOS that allows you to send an email with an attachment. For this, I create a static library in a MailPlugin class. Here is the header and .m file.
#import <Foundation/Foundation.h>
#import <MessageUI/MFMailComposeViewController.h>
@interface MailPlugin : UIViewController <MFMailComposeViewControllerDelegate>{
UIViewController *viewController;
}
-(id)init:(UIViewController*)viewControllerValue;
-(void)sendMailWithAttachment:(NSString*)subject Body:(NSString *)body Attachment:(NSString *)attachment;
-(void) mailComposeController:(MFMailComposeViewController *)controller didFinishWithResult:(MFMailComposeResult)result error:(NSError *)error;
@end
#import "MailPlugin.h"
@implementation MailPlugin
-(id)init:(UIViewController*)viewControllerValue {
self = [super init];
if (self){
viewController = viewControllerValue;
}
NSLog(@"Init viewController: %@",viewController);
return self;
}
-(void)sendMailWithAttachment:(NSString*)subject Body:(NSString *)body Attachment:(NSString *)attachment{
if ([MFMailComposeViewController canSendMail]) {
MFMailComposeViewController *mailController = [[MFMailComposeViewController alloc] init];
mailController.mailComposeDelegate = self;
// Create data to attach
// NSData *data = [[NSFileManager defaultManager] contentsAtPath:attachment];
// Create the file name
// NSString *name = [attachment substringFromIndex:[attachment rangeOfString:@"/" options:NSBackwardsSearch].location + 1];
// Add informations to mail controller
[mailController setSubject:subject];
[mailController setMessageBody:body isHTML:false];
// [mailController addAttachmentData:data mimeType:@"message/rfc822" fileName:name];
NSLog(@"Fin preparation mail");
[viewController presentViewController:mailController animated:YES completion:NULL];
NSLog(@"Affichage mail");
}
else {
NSLog(@"Device is unable to send email");
}
}
-(void) mailComposeController:(MFMailComposeViewController *)controller didFinishWithResult:(MFMailComposeResult)result error:(NSError *)error{
switch (result) {
case MFMailComposeResultCancelled:
NSLog(@"Mail cancelled");
break;
case MFMailComposeResultSaved:
NSLog(@"Mail saved");
break;
case MFMailComposeResultSent:
NSLog(@"Mail sent");
break;
case MFMailComposeResultFailed:
NSLog(@"Mail failed");
break;
default:
break;
}
[viewController dismissViewControllerAnimated:YES completion:NULL];
}
@end
For momenent I commented the part to manage the attachment function sendMailWithAttachment. I then use a wrapper on the Assets/Plugins/iOS folder, here .h and .mm.
#import "ClipboardPlugin.h"
#import "PhotosPlugin.h"
#import "ToastPlugin.h"
#import "MailPlugin.h"
extern "C" void copyStringIntoClipboard_Helper(char *text);
extern "C" void downloadImageInPhotos_Helper(char *filePath);
extern "C" void displayTextToast_Helper(char *text, double duration);
extern "C" void sendMailWithAttachment_Helper(char *subject, char *body, char *attachment);
#import "PluginsiOSWrapper.h"
extern UIView *UnityGetGLView();
extern UIViewController *UnityGetGLViewController();
void copyStringIntoClipboard_Helper(char *text){
[ClipboardPlugin copyStringIntoClipboard:[NSString stringWithUTF8String:text]];
}
void downloadImageInPhotos_Helper(char *filePath){
[PhotosPlugin downloadImageInPhotos:[NSString stringWithUTF8String:filePath]];
}
void displayTextToast_Helper(char *text, double duration){
[ToastPlugin displayTextToast:[NSString stringWithUTF8String:text] View:UnityGetGLView() Duration:duration];
}
static MailPlugin *mp = NULL;
void sendMailWithAttachment_Helper(char *subject, char *body, char *attachment){
if (mp==NULL){
mp = [[MailPlugin alloc] init:UnityGetGLViewController()];
}
[mp sendMailWithAttachment:[NSString stringWithUTF8String:subject] Body:[NSString stringWithUTF8String:body] Attachment:[NSString stringWithUTF8String:attachment]];
}
When I launch my application and I press the button to call the method sendMailWithAttachment_Helper PluginiOSWrapper.mm file, the email view appears fine but I can't do anything without crash with a message "Terminate due to memory pressure ". Here logs Xcode when I clicked on the button:
2014-05-21 16:44:01.641 Prototype[11126:60b] Init viewController: <UnityDefaultViewController: 0x156851a0>
2014-05-21 16:44:02.227 Prototype[11126:60b] Fin preparation mail
2014-05-21 16:44:02.482 Prototype[11126:60b] Affichage mail
2014-05-21 16:44:19.627 Prototype[11126:60b] Received memory warning.
WARNING -> applicationDidReceiveMemoryWarning()
2014-05-21 16:44:20.270 Prototype[11126:60b] Received memory warning.
WARNING -> applicationDidReceiveMemoryWarning()
2014-05-21 16:44:20.402 Prototype[11126:60b] Received memory warning.
WARNING -> applicationDidReceiveMemoryWarning()
2014-05-21 16:44:37.280 Prototype[11126:60b] Received memory warning.
WARNING -> applicationDidReceiveMemoryWarning()
2014-05-21 16:44:37.697 Prototype[11126:60b] Received memory warning.
WARNING -> applicationDidReceiveMemoryWarning()
I can not find the problem, would anyone have a solution? Thank you in advance Noémie
Your answer
Follow this Question
Related Questions
A node in a childnode? 1 Answer
Issue with glDrawArrays in plugin 2 Answers
iOS Plugin - Pass a struct from C++ to C# 1 Answer
Admob Plugin Unity IOS 4 Answers