-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathMessageUI+Convenience.m
133 lines (98 loc) · 5.27 KB
/
MessageUI+Convenience.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
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
//
// MessageUI+Convenience.m
// Ringtonic
//
// Created by Alexander Ivanov on 14.11.16.
// Copyright © 2016 Alexander Ivanov. All rights reserved.
//
#import "MessageUI+Convenience.h"
@implementation MFMailComposeViewController (Convenience)
+ (instancetype)createWithRecipients:(NSArray *)recipients subject:(NSString *)subject body:(NSString *)body attachments:(NSDictionary<NSString *,NSData *> *)attachments {
if (![self canSendMail])
return Nil;
MFMailComposeViewController *instance = [self new];
[instance setToRecipients:recipients];
if (subject)
[instance setSubject:subject];
if (body)
[instance setMessageBody:body isHTML:NO];
for (NSString *name in attachments.allKeys)
[instance addAttachmentData:attachments[name] mimeType:[UTUniformType mimeTypeFromFilenameExtension:name.pathExtension] ?: @"application/octet-stream" fileName:name];
return instance;
}
+ (instancetype)createWithRecipients:(NSArray *)recipients subject:(NSString *)subject body:(NSString *)body {
return [self createWithRecipients:recipients subject:subject body:body attachments:Nil];
}
+ (instancetype)createWithRecipients:(NSArray *)recipients subject:(NSString *)subject {
return [self createWithRecipients:recipients subject:subject body:Nil attachments:Nil];
}
+ (instancetype)createWithRecipients:(NSArray *)recipients {
return [self createWithRecipients:recipients subject:Nil body:Nil attachments:Nil];
}
+ (instancetype)createWithRecipient:(NSString *)recipient subject:(NSString *)subject body:(NSString *)body {
return [self createWithRecipients:arr_(recipient) subject:subject body:body attachments:Nil];
}
+ (instancetype)createWithRecipient:(NSString *)recipient subject:(NSString *)subject {
return [self createWithRecipients:arr_(recipient) subject:subject body:Nil attachments:Nil];
}
+ (instancetype)createWithRecipient:(NSString *)recipient {
return [self createWithRecipients:arr_(recipient) subject:Nil body:Nil attachments:Nil];
}
@end
@interface MFMailCompose ()
@property (copy, nonatomic) void(^completionHandler)(MFMailComposeResult result, NSError *error);
@end
@implementation MFMailCompose
- (instancetype)init {
self = [super init];
if (self)
self.mailComposeDelegate = self;
return self;
}
- (void)mailComposeController:(MFMailComposeViewController *)controller didFinishWithResult:(MFMailComposeResult)result error:(NSError *)error {
if (self.completionHandler)
self.completionHandler(result, error);
[controller dismissViewControllerAnimated:YES completion:Nil];
[error log:@"mailComposeController:didFinishWithResult:error:"];
}
@end
@implementation NSURL (MessageUI)
+ (NSURL *)URLWithRecipients:(NSArray<NSString *> *)recipients subject:(NSString *)subject body:(NSString *)body {
NSMutableDictionary *parameters = [NSMutableDictionary new];
parameters[@"subject"] = subject;
parameters[@"body"] = body;
return [[NSURL URLWithString:[NSString stringWithFormat:@"mailto:%@", [recipients componentsJoinedByString:STR_SEMICOLON]]] URLByAppendingQueryDictionary:parameters];
}
@end
@implementation UIViewController (MFMailComposeViewController)
- (MFMailComposeViewController *)presentMailComposeWithRecipients:(NSArray<NSString *> *)recipients subject:(NSString *)subject body:(NSString *)body attachments:(NSDictionary<NSString *,NSData *> *)attachments completionHandler:(void (^)(MFMailComposeResult, NSError *))completionHandler {
MFMailCompose *vc = [MFMailCompose createWithRecipients:recipients subject:subject body:body attachments:attachments];
vc.completionHandler = completionHandler;
if (vc)
[self presentViewController:vc animated:YES completion:Nil];
else
[UIApplication openURL:[NSURL URLWithRecipients:recipients subject:subject body:body] options:Nil completionHandler:^(BOOL success) {
if (completionHandler)
completionHandler(MFMailComposeResultFailed, Nil);
}];
return vc;
}
- (MFMailComposeViewController *)presentMailComposeWithRecipients:(NSArray<NSString *> *)recipients subject:(NSString *)subject body:(NSString *)body {
return [self presentMailComposeWithRecipients:recipients subject:subject body:body attachments:Nil completionHandler:Nil];
}
- (MFMailComposeViewController *)presentMailComposeWithRecipients:(NSArray<NSString *> *)recipients subject:(NSString *)subject {
return [self presentMailComposeWithRecipients:recipients subject:subject body:Nil attachments:Nil completionHandler:Nil];
}
- (MFMailComposeViewController *)presentMailComposeWithRecipients:(NSArray<NSString *> *)recipients {
return [self presentMailComposeWithRecipients:recipients subject:Nil body:Nil attachments:Nil completionHandler:Nil];
}
- (MFMailComposeViewController *)presentMailComposeWithRecipient:(NSString *)recipient subject:(NSString *)subject body:(NSString *)body {
return [self presentMailComposeWithRecipients:arr_(recipient) subject:subject body:body attachments:Nil completionHandler:Nil];
}
- (MFMailComposeViewController *)presentMailComposeWithRecipient:(NSString *)recipient subject:(NSString *)subject {
return [self presentMailComposeWithRecipients:arr_(recipient) subject:subject body:Nil attachments:Nil completionHandler:Nil];
}
- (MFMailComposeViewController *)presentMailComposeWithRecipient:(NSString *)recipient {
return [self presentMailComposeWithRecipients:arr_(recipient) subject:Nil body:Nil attachments:Nil completionHandler:Nil];
}
@end