-
Notifications
You must be signed in to change notification settings - Fork 0
/
EmailController.m
93 lines (84 loc) · 2.92 KB
/
EmailController.m
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
//
// Copyright (c) 2012 Norman Basham
// http://www.apache.org/licenses/LICENSE-2.0
//
#import "EmailController.h"
#import <QuartzCore/QuartzCore.h>
@implementation EmailController
@synthesize attachment;
@synthesize attachmentName;
@synthesize recipients;
@synthesize subject;
@synthesize body;
@synthesize isHTML;
@synthesize logResult;
- (id)init {
self = [super init];
if (self) {
attachment = nil;
attachmentName = @"email attachment";
recipients = [NSMutableArray array];
subject = nil;
body = nil;
isHTML = YES;
logResult = YES;
}
return self;
}
-(void)addRecipient:(NSString*)recipient {
[recipients addObject:recipient];
}
-(void)attachImage:(UIImage*)image {
self.attachment = [NSData dataWithData:UIImagePNGRepresentation(image)];
self.mimeType = @"image/png";
}
-(void)attachViewScreenshot:(UIView*)v {
CGFloat deviceMainScreenScale = 0.0;
BOOL optimizationExcludeAlpha = NO;
UIGraphicsBeginImageContextWithOptions(v.bounds.size, optimizationExcludeAlpha, deviceMainScreenScale);
[v.layer renderInContext:UIGraphicsGetCurrentContext()];
UIImage* image = UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext();
[self attachImage:image];
}
-(void)sendEmail:(UIViewController*)vc {
@try {
MFMailComposeViewController* picker = [[MFMailComposeViewController alloc] init];
picker.mailComposeDelegate = (id<MFMailComposeViewControllerDelegate>)self;
[picker setSubject:self.subject];
[picker setMessageBody:self.body isHTML:YES];
[picker setToRecipients:self.recipients];
if(self.attachment != nil) {
[picker addAttachmentData:self.attachment mimeType:self.mimeType fileName:self.attachmentName];
}
[vc presentModalViewController:picker animated:YES];
}
@catch (NSException* e) {
if(self.logResult) {
NSLog(@"sendEmail failed.");
}
}
}
-(void)mailComposeController:(MFMailComposeViewController*)controller didFinishWithResult:(MFMailComposeResult)result error:(NSError*)error {
[controller dismissModalViewControllerAnimated:YES];
NSString* resultStr = @"unknow email response";
switch (result) {
case MFMailComposeResultCancelled:
resultStr = @"Email composer reports user canceled.";
break;
case MFMailComposeResultSaved:
resultStr = @"Email composer reports user saved";
break;
case MFMailComposeResultSent:
// doesn't appear to be away to get sent parts e.g. recipients and body
resultStr = @"Email composer reports user sent.";
break;
case MFMailComposeResultFailed:
resultStr = [NSString stringWithFormat:@"Email composer reports email failed. Error %@", [error description]];
break;
}
if(self.logResult) {
NSLog(@"%@", resultStr);
}
}
@end