IOS: A simple example for sending messages and text messages

Source: Internet
Author: User

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

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.