Han junqiang's blog on iOS

Source: Internet
Author: User

Han junqiang's blog on iOS
Framework needs to be introduced:

MessageUI. framework

The layout is as follows:

 

SMS and email:

 

# Import ViewController. h # import
 
  
@ Interface ViewController ()
  
   
// Follow the protocol @ end @ implementation ViewController
  
 

SMS function:

 

 

// Message function-(IBAction) messageButtonAction :( UIButton *) sender {# send a text message outside The pragma mark Program/* // defines the url for opening the text message. Keyword: sms: NSURL * url = [NSURL URLWithString: [NSString stringWithFormat: @ sms: // % @, @ 10086]; // determine whether the program can call the function of opening the text message. if ([[UIApplication sharedApplication] canOpenURL: url]) {[[UIApplication sharedApplication] openURL: url];} else {UIAlertView * alert = [[UIAlertView alloc] initWithTitle: @ prompt message: @ your device does not support this SMS function delegate: self cancelButtonTitle: @ confirm otherButtonTitles: nil, nil]; [alert show];} * // * use openURL to enable the short message function in the program. You need to use the Keyword: sms:, followed by the phone number to be sent. Disadvantages: 1. this method will jump out of our running program and open the system's text message interface. However, when you close the text message, you cannot return to the program. 2. in this method, we can only define the mobile phone number to be sent and cannot edit the content of the sent text message ;*/}

 

# Send text messages in The pragma mark Program

/*

To make up for the shortcomings of the above two methods, you need another method to use the short message function: use the short message function in the program.

*/

 

// 1. framework required for adding text messages: MessageUI. framework

// 2. Introduce the header file to implement the following code:

// 3. Determine whether a text message can be sent.

-(IBAction) messageButtonAction :( UIButton *) sender {# send sms bool canSendMessage = [MFMessageComposeViewController canSendText] Outside The pragma mark program; if (canSendMessage) {// create the SMS View Controller MFMessageComposeViewController * messageVC = [[MFMessageComposeViewController alloc] init]; // set the proxy messageVC. messageComposeDelegate = self; // set the message content messageVC. body = @ to get a message; // set the phone number, which is an array. You can set multiple phone numbers to implement the group function messageVC. recipients = @ [@ 10086, @ 10010]; // you can use this method to open an SMS interface in the program. [self presentViewController: messageVC animated: YES completion: nil];} else {UIAlertView * alert = [[UIAlertView alloc] initWithTitle: @ prompt message: @ your device does not support this text message function delegate: self cancelButtonTitle: @ confirm otherButtonTitles: nil, nil]; [alert show] ;}}

Information proxy method:

 

-(Void) messageComposeViewController :( MFMessageComposeViewController *) controller didFinishWithResult :( MessageComposeResult) result {// The enumerated value of MessageComposeResult: // cancel, // cancel, // send SMS // response // if the message fails to be sent (result = MessageComposeResultCancelled | result = MessageComposeResultSent) {[self dismissViewControllerAnimated: YES completion: nil];}

 

Email function:

// Email function-(IBAction) mailButtonAction :( UIButton *) sender {# Send email outside The pragma mark Program/* // open the system email page, mailto: NSURL * mailURL = [NSURL URLWithString: [NSString stringWithFormat: @ mailto: % @, @ 13683799303@163.com]; // cc: cc object subject: topic body: content // NSURL * mailURL2 = [NSURL URLWithString: [NSString stringWithFormat: @ mailto: % @? Cc =%@ & subject =%@ & body =%@, @ 13683799303@163.com, @ 13683799303@26.com, @ email, @ hello!]; If ([[UIApplication sharedApplication] canOpenURL: mailURL]) {[[UIApplication sharedApplication] openURL: mailURL];} else {UIAlertView * alert = [[UIAlertView alloc] initWithTitle: @ prompt message: @ your device does not support the mail function delegate: self cancelButtonTitle: @ OK otherButtonTitles: nil, nil]; [alert show];} * // * similar to the preceding text message, this method also jumps out of the program and calls the system's email interface; */# send an email in The pragma mark program // determine whether the email can be sent. BOOL canSendMail = [MFMailComposeViewController canSendMail]; if (canSendMail) {// Create mail View Controller MFMailComposeViewController * mailVC = [[MFMailComposeViewController alloc] init]; // set the recipient, array, can realize mass [mailVC setToRecipients: @ [@ 13683799303@163.com, @ 135895587@qq.com]; // sets the CC object, [mailVC setCcRecipients: @ [@ 13683799303@163.com, @ 135895587@qq.com]; // sets BCC [mailVC setBccRecipients: @ [@ 13683799303@163.com]; // set content [mailVC setMessageBody: @ nice to know you isHTML: NO]; // set proxy mailVC. mailComposeDelegate = self; // enable the mail function [self presentViewController: mailVC animated: YES completion: nil];} else {UIAlertView * alert = [[UIAlertView alloc] initWithTitle: @ message: @ your device does not support the mail function delegate: self cancelButtonTitle: @ OK otherButtonTitles: nil, nil]; [alert show];}

Email proxy method:

 

-(Void) mailComposeController :( MFMailComposeViewController *) controller didFinishWithResult :( MFMailComposeResult) result error :( NSError *) error {// abort, cancel sending // complete, save // MFMailComposeResultSent, send // MFMailComposeResultFailed failed to send switch (result) {case MFMailComposeResultCancelled: NSLog (@ cancel sending); break; case when: NSLog (@ save); break; case MFMailComposeResultSent: NSLog (@ sent successfully); break; case MFMailComposeResultFailed: NSLog (@ failed); break; default: break;} [self dismissViewControllerAnimated: YES completion: nil];}

Final effect: (because the simulator cannot demonstrate how to send text messages, the following phenomenon occurs)

 


 


 

Related Article

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.