Created
October 17, 2014 16:20
-
-
Save atkit/b85333d6aac14b55c158 to your computer and use it in GitHub Desktop.
Mail Compose Builder - Easy to use helper to present MailComposer, includes convenient category for UIViewController
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// Created by Andrew on 24.08.13. | |
// Copyright (c) 2013 Andrew Tokarev. All rights reserved. | |
// | |
#import <Foundation/Foundation.h> | |
#import <MessageUI/MessageUI.h> | |
@interface MailComposerBuilder : NSObject<MFMailComposeViewControllerDelegate> | |
+ (id) builder; | |
- (id) withParentController:(UIViewController*)parentViewController; | |
- (id) withText:(NSString*)text; | |
- (id) withHtml:(NSString*)html; | |
- (id) withSubject:(NSString*)subject; | |
- (id) withToRecipient:(NSString*)recipient; | |
- (void) present; | |
@end | |
@interface UIViewController(MailComposerBuilder) | |
- (void) presentMailComposerWithRecipient:(NSString*)recipient subject:(NSString*)subject; | |
- (void) presentMailComposerWithSubject:(NSString*)subject text:(NSString*)text; | |
@end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// Created by Andrew on 24.08.13. | |
// Copyright (c) 2013 Andrew Tokarev. All rights reserved. | |
// | |
#import "MailComposerBuilder.h" | |
@interface MailComposerBuilder(/*Private*/) | |
@property (nonatomic, strong) UIViewController * parentViewController; | |
@property (nonatomic, strong) MFMailComposeViewController * mailViewController; | |
@property (nonatomic, strong) id this; | |
@end | |
@implementation MailComposerBuilder { | |
MFMailComposeViewController * mailComposeViewController; | |
} | |
- (void) present | |
{ | |
if ([self isUnableToPresentMailComposer]) { | |
[self showAlertAboutMailUnavaliability]; | |
} | |
self.this = self; | |
[self.parentViewController presentViewController:self.mailViewController | |
animated:YES | |
completion:NULL]; | |
} | |
- (BOOL) isUnableToPresentMailComposer | |
{ | |
return !(self.parentViewController && self.mailViewController); | |
} | |
#pragma mark - MFMailComposeViewControllerDelegate | |
- (void)mailComposeController:(MFMailComposeViewController*)controller didFinishWithResult:(MFMailComposeResult)result error:(NSError*)error | |
{ | |
if (result == MFMailComposeResultFailed) { | |
[self showAlertAboutMailSendingFailure]; | |
} | |
[controller dismissViewControllerAnimated:YES completion:NULL]; | |
self.this = nil; | |
} | |
#pragma mark - | |
+ (id) builder | |
{ | |
return [self new]; | |
} | |
- (id) withParentController:(UIViewController*)parentViewController | |
{ | |
self.parentViewController = parentViewController; | |
return self; | |
} | |
- (id) withText:(NSString*)text | |
{ | |
[self.mailViewController setMessageBody:text isHTML:NO]; | |
return self; | |
} | |
- (id) withHtml:(NSString*)html | |
{ | |
[self.mailViewController setMessageBody:html isHTML:YES]; | |
return self; | |
} | |
- (id) withSubject:(NSString*)subject | |
{ | |
[self.mailViewController setSubject:subject]; | |
return self; | |
} | |
- (id) withToRecipient:(NSString*)recipient | |
{ | |
[self.mailViewController setToRecipients:@[recipient]]; | |
return self; | |
} | |
#pragma mark - | |
- (MFMailComposeViewController *)mailViewController | |
{ | |
if (![MFMailComposeViewController canSendMail]) | |
{ | |
return nil; | |
} | |
if(!_mailViewController) { | |
_mailViewController = [MFMailComposeViewController new]; | |
_mailViewController.mailComposeDelegate = self; | |
_mailViewController.modalPresentationStyle = UIModalPresentationPageSheet; | |
} | |
return _mailViewController; | |
} | |
#pragma mark - Alerts | |
- (void)showAlertAboutMailUnavaliability | |
{ | |
[self showAlertWithMessage:NSLocalizedString(@"Mail is not configured on this device", @"MailComposer Mail Unavaliability Message")]; | |
} | |
- (void)showAlertAboutMailSendingFailure | |
{ | |
[self showAlertWithMessage:NSLocalizedString(@"Mail was not sent", @"MailComposer Mail Failure Message")]; | |
} | |
- (void)showAlertWithMessage:(NSString*)message | |
{ | |
[[[UIAlertView alloc]initWithTitle:NSLocalizedString(@"Mail", @"MailComposer Alert Title") | |
message:message | |
delegate:nil | |
cancelButtonTitle:@"OK" otherButtonTitles:nil]show]; | |
} | |
@end | |
@implementation UIViewController(MailComposerBuilder) | |
- (void) presentMailComposerWithRecipient:(NSString*)recipient subject:(NSString*)subject | |
{ | |
[[[[[MailComposerBuilder builder] withParentController:self] withToRecipient:recipient] withSubject:subject] present]; | |
} | |
- (void) presentMailComposerWithSubject:(NSString*)subject text:(NSString*)text; | |
{ | |
[[[[[MailComposerBuilder builder] withParentController:self] withSubject:subject] withText:text] present]; | |
} | |
@end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment