Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 13 Next capture
2021 2022 2023
1 capture
13 Jun 22 - 13 Jun 22
sparklines
Close Help
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
avatar image
0
Question by Noemie · May 22, 2014 at 07:23 AM · iospluginmail

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

Comment
Add comment
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users

0 Replies

· Add your reply
  • Sort: 

Your answer

Hint: You can notify a user about this post by typing @username

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Follow this Question

Answers Answers and Comments

20 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

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

Alternate to UnitySendMessage in iOS native 1 Answer


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges