Send mail
1. Import library file: Messageui.framework
2. Introduction of header Files
3. Implement Agent <MFMailComposeViewControllerDelegate> and <UINavigationControllerDelegate>
code example:
Copy Code code as follows:
-(void) didclicksendemailbuttonaction{
if ([mfmailcomposeviewcontroller cansendmail] = YES) {
Mfmailcomposeviewcontroller *MAILVC = [[Mfmailcomposeviewcontroller alloc] init];
Set Agent (unlike the previous agent, not "delegate", must not forget Ah, Agent has 3 steps)
Mailvc.mailcomposedelegate = self;
Recipient
Nsarray *sendtoperson = @[@ "humingtao2014@gmail.com"];
[MAILVC Settorecipients:sendtoperson];
Cc
Nsarray *copytoperson = @[@ "humingtao2013@126.com"];
[MAILVC Setccrecipients:copytoperson];
Secret Send
Nsarray *secrettoperson = @[@ "563821250@qq.com"];
[MAILVC Setbccrecipients:secrettoperson];
Theme
[MAILVC setsubject:@ "Hello World"];
[Self PRESENTVIEWCONTROLLER:MAILVC animated:yes completion:nil];
[MAILVC setmessagebody:@ "legacy, haha hehe rattling" ishtml:no];
}else{
NSLog (@ "This device does not support mail forwarding");
}
}
-(void) Mailcomposecontroller: (Mfmailcomposeviewcontroller *) controller didfinishwithresult: (mfmailcomposeresult) Result Error: (Nserror *) error{
Switch (Result) {
Case mfmailcomposeresultcancelled:
NSLog (@ "Cancel sending");
Break
Case mfmailcomposeresultfailed:
NSLog (@ "send failed");
Break
Case mfmailcomposeresultsaved:
NSLog (@ "save draft document");
Break
Case Mfmailcomposeresultsent:
NSLog (@ "send success");
Break
Default
Break
}
[Self dismissviewcontrolleranimated:yes completion:nil];
}
The system sends, the simulator does not support, must use the real machine test
-(void) didclicksendsystememailbuttonaction{
Nsurl *url = [Nsurl urlwithstring:@ "humingtao2014@gmail.com"];
if ([[[UIApplication sharedapplication] canopenurl:url] = YES) {
[[UIApplication sharedapplication] openurl:url];
}else{
NSLog (@ "This device is not supported");
}
}
Send SMS
the previous three steps introduce configuration and send as mail
Copy Code code as follows:
Call system API Send SMS
-(void) didclicksendmessagebuttonaction{
if ([mfmessagecomposeviewcontroller cansendtext] = YES) {
Mfmessagecomposeviewcontroller *MESSAGEVC = [[Mfmessagecomposeviewcontroller alloc] init];
Set up Agent <MFMessageComposeViewControllerDelegate>
Messagevc.messagecomposedelegate = self;
Send to WHO
messagevc.recipients = @[@ "18757289870"];
Messagevc.body = @ "Hello world";
[Self PRESENTVIEWCONTROLLER:MESSAGEVC animated:yes completion:nil];
}else{
NSLog (@ "This device is not supported");
}
}
-(void) Messagecomposeviewcontroller: (Mfmessagecomposeviewcontroller *) controller didfinishwithresult: ( Messagecomposeresult) result{
Switch (Result) {
Case messagecomposeresultcancelled:
NSLog (@ "Cancel sending");
Break
Case messagecomposeresultfailed:
NSLog (@ "send failed");
Break
Case Messagecomposeresultsent:
NSLog (@ "send success");
Break
Default
Break
}
[Self dismissviewcontrolleranimated:yes completion:nil];
}
Calling the system application to send a message
-(void) didclicksendmessage2buttonaction{
Nsurl *url = [Nsurl urlwithstring:@ "sms:18656348970"];
if ([[[UIApplication sharedapplication] canopenurl:url] = YES) {
[[UIApplication sharedapplication] openurl:url];
}else{
NSLog (@ "failure");
}
}