Skip to content

Instantly share code, notes, and snippets.

@markrickert
Created August 8, 2014 14:22
Show Gist options
  • Save markrickert/347d512110f5db0be8d8 to your computer and use it in GitHub Desktop.
Save markrickert/347d512110f5db0be8d8 to your computer and use it in GitHub Desktop.
MFMailComposeViewController extracted from UIViewController
#import <UIKit/UIKit.h>
#import <MessageUI/MessageUI.h>
#import <MessageUI/MFMailComposeViewController.h>
@interface MailMan : NSObject <MFMailComposeViewControllerDelegate> {
}
@property (nonatomic) UIViewController *vc;
- (id)initWithParentVC:(UIViewController *)vc;
- (void)emailWithSubject: (NSString *)subject andHTML:(NSString*)html;
- (void)emailWithSubject: (NSString *)subject andText:(NSString*)text;
- (void)emailWithSubject: (NSString *)subject andMessage:(NSString*)string isHTML:(BOOL)html;
@end
#import "mailman.h"
@implementation MailMan
- (id)initWithParentVC:(UIViewController *)vc
{
self = [super init];
if(self) {
NSLog(@"_init: %@", self);
self.vc = vc;
}
return self;
}
- (void)emailWithSubject: (NSString *)subject andText:(NSString*)text {
[self emailWithSubject:subject andMessage:text isHTML:NO];
}
- (void)emailWithSubject: (NSString *)subject andHTML:(NSString*)html {
[self emailWithSubject:subject andMessage:html isHTML:YES];
}
- (void)emailWithSubject: (NSString *)subject andMessage:(NSString*)string isHTML:(BOOL)isHTML
{
if ([MFMailComposeViewController canSendMail]) {
MFMailComposeViewController *mailViewController = [[MFMailComposeViewController alloc] init];
mailViewController.mailComposeDelegate = self;
[mailViewController setSubject:subject];
[mailViewController setMessageBody:string isHTML:isHTML];
[self.vc presentViewController:mailViewController animated:YES completion:nil];
} else {
NSLog(@"Device is unable to send email in its current state.");
UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"No Email Account" message:@"You have not configured this device for sending email." delegate:nil cancelButtonTitle:@"OK" otherButtonTitles:nil];
[alert show];
}
}
- (void)mailComposeController:(MFMailComposeViewController*)controller didFinishWithResult:(MFMailComposeResult)result error:(NSError*)error {
UIAlertView *alert;
switch (result) {
case MFMailComposeResultCancelled:
NSLog(@"Mail Cancelled");
alert = [[UIAlertView alloc] initWithTitle:@"Email Feedback:" message:@"Mail Cancelled." delegate:nil cancelButtonTitle:@"OK" otherButtonTitles:nil];
[alert show];
break;
case MFMailComposeResultSaved:
NSLog(@"Mail Saved");
alert = [[UIAlertView alloc] initWithTitle:@"Email Feedback:" message:@"Mail Saved." delegate:nil cancelButtonTitle:@"OK" otherButtonTitles:nil];
[alert show];
break;
case MFMailComposeResultSent:
NSLog(@"Sent Mail");
alert = [[UIAlertView alloc] initWithTitle:@"Email Feedback:" message:@"Sent Mail." delegate:nil cancelButtonTitle:@"OK" otherButtonTitles:nil];
[alert show];
break;
case MFMailComposeResultFailed:
NSLog(@"Mail Failed");
NSLog(@"%@", error.localizedDescription);
alert = [[UIAlertView alloc] initWithTitle:@"Email Feedback:" message:[NSString stringWithFormat:@"Mail Failed: %@", error.localizedDescription] delegate:nil cancelButtonTitle:@"OK" otherButtonTitles:nil];
[alert show];
break;
default:
break;
}
[self.vc dismissViewControllerAnimated:YES completion:nil];
}
@end

Usage:

# RubyMotion:
# In your rakefile:
app.vendor_project("vendor/MailMan", :static, :cflags => '-fobjc-arc')

# In your View Controller:
@mailman ||= MailMan.alloc.initWithParentVC(WeakRef.new(self))
@mailman.emailWithSubject('Subject', andText: 'body')
mailman = [[MailMan alloc] initWithParentVC:self];
[mailman emailWithSubject:'Subject' andText: 'body'];
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment