Send mail
1. Import library file: Messageui.framework
2. Introduction of header Files
3. Implement Agent <MFMailComposeViewControllerDelegate> and <UINavigationControllerDelegate>
code example:
- (void) didclicksendemailbuttonaction{if([mfmailcomposeviewcontroller cansendmail] = =YES) {Mfmailcomposeviewcontroller*MAILVC =[[Mfmailcomposeviewcontroller alloc] init]; //set up the agent (unlike the previous agent, not "delegate", must not forget, the agent has 3 steps)Mailvc.mailcomposedelegate =Self ; //RecipientNsarray *sendtoperson = @[@"[email protected]"]; [MAILVC Settorecipients:sendtoperson]; //ccNsarray *copytoperson = @[@"[email protected]"]; [MAILVC Setccrecipients:copytoperson]; //Secret DeliveryNsarray *secrettoperson = @[@"[email protected]"]; [MAILVC Setbccrecipients:secrettoperson]; //Theme[MAILVC Setsubject:@"Hello World"]; [Self PRESENTVIEWCONTROLLER:MAILVC animated:yes completion:nil]; [MAILVC setmessagebody:@"魑魅魍魉, haha hehe hey rattling"Ishtml:no]; }Else{NSLog (@"This device does not support mail sending"); } } - (void) Mailcomposecontroller: (Mfmailcomposeviewcontroller *) controller didfinishwithresult: (mfmailcomposeresult) result Error: (Nserror *) error{Switch(Result) { CaseMfmailcomposeresultcancelled:nslog (@"Cancel Send"); Break; CaseMfmailcomposeresultfailed:nslog (@"Send failed"); Break; CaseMfmailcomposeresultsaved:nslog (@"save a draft file"); Break; CaseMfmailcomposeresultsent:nslog (@"sent successfully"); Break; default: Break; } [self Dismissviewcontrolleranimated:yes completion:nil]; } //system sent, simulator not supported, to be tested with real machine- (void) didclicksendsystememailbuttonaction{Nsurl*url = [Nsurl urlwithstring:@"[email protected]"]; if([[UIApplication sharedapplication] canopenurl:url] = =YES) {[[UIApplication sharedapplication] openurl:url]; }Else{NSLog (@"This device does not support"); } }
Send SMS
The previous three steps introduced the same configuration and mail delivery
//Call system API to send SMS- (void) didclicksendmessagebuttonaction{if([mfmessagecomposeviewcontroller cansendtext] = =YES) {Mfmessagecomposeviewcontroller*MESSAGEVC =[[Mfmessagecomposeviewcontroller alloc] init]; //set up proxy <MFMessageComposeViewControllerDelegate>Messagevc.messagecomposedelegate =Self ; //send to WHOMessagevc.recipients = @[@"18757289870"]; Messagevc.body=@"Hello World"; [Self PRESENTVIEWCONTROLLER:MESSAGEVC animated:yes completion:nil]; }Else{NSLog (@"This device does not support"); } } - (void) Messagecomposeviewcontroller: (Mfmessagecomposeviewcontroller *Controller Didfinishwithresult: (messagecomposeresult) result{Switch(Result) { CaseMessagecomposeresultcancelled:nslog (@"Cancel Send"); Break; CaseMessagecomposeresultfailed:nslog (@"Send failed"); Break; CaseMessagecomposeresultsent:nslog (@"sent successfully"); Break; default: Break; } [self Dismissviewcontrolleranimated:yes completion:nil]; } //invoking a 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 (@"failed"); } }
IOS: A simple example for sending messages and text messages