Send Sms/email implementation in iOS Han Junqiang blog

Source: Internet
Author: User
<span id="Label3"></p><span style="font-family:Microsoft YaHei; font-size:18px; color:#ff0000"><span style="font-family:Microsoft YaHei; font-size:18px; color:#ff0000">need to introduce a framework:</span></span><p style="margin-top:0px; margin-bottom:0px"><p style="margin-top:0px; margin-bottom:0px"><span style="font-family:Microsoft YaHei; font-size:18px; color:#ff0000">Messageui.framework</span></p></p><p style="margin-top:0px; margin-bottom:0px"><p style="margin-top:0px; margin-bottom:0px"><span style="font-family:Microsoft YaHei; font-size:18px; color:#ff0000">The layout is as Follows:</span></p></p><p style="margin-top:0px; margin-bottom:0px"><p style="margin-top:0px; margin-bottom:0px"><span style="font-family:Microsoft YaHei; font-size:18px; color:#ff0000"><br></span></p></p><p style="margin-top:0px; margin-bottom:0px"><p style="margin-top:0px; margin-bottom:0px"><span style="font-family:Microsoft YaHei; font-size:18px; color:#ff0000"><br></span></p></p><p style="margin-top:0px; margin-bottom:0px"><p style="margin-top:0px; margin-bottom:0px"><span style="font-family:Microsoft YaHei; font-size:18px; color:#ff0000">SMS and Email:</span></p></p><p style="margin-top:0px; margin-bottom:0px"><p style="margin-top:0px; margin-bottom:0px"><span style="font-family:Microsoft YaHei; font-size:18px; color:#ff0000"></span></p></p><pre name="code" class="objc"><pre name="code" class="objc">#import "ViewController.h" #import <MessageUI/MessageUI.h> @interface Viewcontroller () < Mfmessagecomposeviewcontrollerdelegate,mfmailcomposeviewcontrollerdelegate>//follows the Protocol @end@implementation Viewcontroller</pre></pre><br>SMS Features:<p><p></p></p><p style="margin-top:0px; margin-bottom:0px"><p style="margin-top:0px; margin-bottom:0px"><span style="font-family:Microsoft YaHei; font-size:18px"></span></p></p><pre name="code" class="objc" style="color: rgb(255, 0, 0);"><pre name="code" class="objc" style="color: rgb(255, 0, 0);">SMS Function-(ibaction) messagebuttonaction: (UIButton *) Sender {#pragma Mark program to send SMS * ///define the URL to open sms, keyword: sms: Nsurl *url = [nsurl urlwithstring:[nsstring stringwithformat:@ "sms://%@", @ "10086"]; Determine if the program can call open SMS function if ([[[uiapplication sharedapplication] canopenurl:url]) { [[uiapplication sharedapplication] openurl:url]; } else{ Uialertview *alert = [[uialertview alloc]initwithtitle:@ "prompt" message:@ "your device does not support this SMS feature" delegate:self cancelbuttontitle:@ "ok" otherbuttontitles:nil, nil]; [alert show]; } *//* with OpenURL to open the program in the SMS function, need to use the keyword: "sms:", followed by the phone to be sent can be; Cons: 1. This method will jump out of the program we are running, open the System's SMS interface, but when the user closes the text message, unable to return to the Program. 2. This method we can only define the mobile phone number to send, unable to edit the content of the text sent; */ }</pre></pre><p><p></p></p><p style="margin-top:0px; margin-bottom:0px; font-family:‘STHeiti Light‘"><p style="margin-top:0px; margin-bottom:0px; font-family:‘STHeiti Light‘"><span style="color:#3333ff">Send SMS within #pragma mark program</span></p></p><p style="margin-top:0px; margin-bottom:0px; font-family:‘STHeiti Light‘"><p style="margin-top:0px; margin-bottom:0px; font-family:‘STHeiti Light‘"><span style="color:#3333ff">/*</span></p></p><p style="margin-top:0px; margin-bottom:0px; font-family:‘STHeiti Light‘"><p style="margin-top:0px; margin-bottom:0px; font-family:‘STHeiti Light‘"><span style="color:#3333ff">In order to compensate for the deficiencies of the two methods mentioned above, another way to use the SMS function is to use SMS in the Program.</span></p></p><p style="margin-top:0px; margin-bottom:0px; font-family:‘STHeiti Light‘"><p style="margin-top:0px; margin-bottom:0px; font-family:‘STHeiti Light‘"><span style="color:#3333ff">*/</span></p></p><p style="margin-top:0px; margin-bottom:0px; font-family:‘STHeiti Light‘; min-height:20px"><p style="margin-top:0px; margin-bottom:0px; font-family:‘STHeiti Light‘; min-height:20px"><span style="color:#3333ff"></span></p></p><p style="margin-top:0px; margin-bottom:0px; font-family:‘STHeiti Light‘"><p style="margin-top:0px; margin-bottom:0px; font-family:‘STHeiti Light‘"><span style="color:#3333ff">1. Frame required to add sms: messageui.framework</span></p></p><p style="margin-top:0px; margin-bottom:0px; font-family:‘STHeiti Light‘"><p style="margin-top:0px; margin-bottom:0px; font-family:‘STHeiti Light‘"><span style="color:#3333ff">2. Introduce the header file to implement the following code</span></p></p><p style="margin-top:0px; margin-bottom:0px; font-family:‘STHeiti Light‘"><p style="margin-top:0px; margin-bottom:0px font-family: ' Stheiti light '"><span style="color: #3333ff">   //3. decide if you can send text messages </span> </p></p><pre name="code" class="objc"><pre name="code" class="objc">-(ibaction) messagebuttonaction: (UIButton *) Sender {#pragma mark out of the program send SMS BOOL cansendmessage = [ Mfmessagecomposeviewcontroller cansendtext]; If (cansendmessage) { //create SMS View Controller mfmessagecomposeviewcontroller *MESSAGEVC = [[ Mfmessagecomposeviewcontroller alloc]init]; Set proxy messagevc.messagecomposedelegate = self; Set SMS content Messagevc.body = @ "come to a message"; Set up the phone, is an array, can be set up multiple phones, the implementation of mass function messagevc.recipients = @[@ "10086", @ "10010"]; Open the text message function, through this method will open a text message interface within the program; [self PRESENTVIEWCONTROLLER:MESSAGEVC Animated:yes completion:nil]; } else{ Uialertview *alert = [[uialertview alloc]initwithtitle:@ "prompt" message:@ "your device does not support this SMS feature" delegate:self cancelbuttontitle:@ "ok" otherbuttontitles:nil, nil]; [alert show]; } }</pre></pre><br><p style="margin-top:0px; margin-bottom:0px; font-family:‘STHeiti Light‘"><p style="margin-top:0px; margin-bottom:0px; font-family:‘STHeiti Light‘"><span style="color:#ff0000">Proxy methods for Information:</span></p></p><p style="margin-top:0px; margin-bottom:0px; font-family:‘STHeiti Light‘"><p style="margin-top:0px; margin-bottom:0px; font-family:‘STHeiti Light‘"><span style="color:#ff0000"></span></p></p><pre name="code" class="objc"><pre name="code" class="objc">-(void) messagecomposeviewcontroller: (mfmessagecomposeviewcontroller *) Controller didfinishwithresult: ( Messagecomposeresult) Result{ //messagecomposeresult Enumeration value:// messagecomposeresultcancelled,//cancel Send SMS function// messagecomposeresultsent, //send sms// messagecomposeresultfailed //send failed if (result = = messagecomposeresultcancelled | | result = = Messagecomposeresultsent) { [self dismissviewcontrolleranimated:yes completion:nil];} }</pre></pre><br><p><p></p></p><p style="margin-top:0px; margin-bottom:0px; font-family:‘STHeiti Light‘"><p style="margin-top:0px; margin-bottom:0px; font-family:‘STHeiti Light‘">Mail Features:</p></p><pre name="code" class="objc">Mail function-(ibaction) mailbuttonaction: (UIButton *) sender {#pragma Mark out-of-program send mail *///open system mail page, mailto:nsurl *mai Lurl = [nsurl urlwithstring:[nsstring stringwithformat:@ "mailto:%@", @ "[email protected]"]; Cc: cc Object subject: subject body: Content//nsurl *mailurl2 = [nsurl urlwithstring:[nsstring stringwithformat:@ "mailto:%@?cc =%@& Amp;subject =%@&body =%@ ", @" [email protected] ", @" [email protected] ", @" mail ", @" hello! "]; If ([[[uiapplication sharedapplication] canopenurl:mailurl]) {[[uiapplication sharedapplication] openURL:mailURL]; }else{uialertview *alert = [[uialertview alloc]initwithtitle:@ ' Prompt ' message:@ ' your device does not support mail function ' delegate:self cancelbuttontitle:@ "ok" otherbuttontitles:nil, nil]; [alert show]; } *//* This method to send the message as above, will also jump out of the program, call the System's mail interface; */#pragma Mark Program Send mail//determine if Mail can be sent BOOL Cansendmail = [mfmailcomposeviewcontroller cansendmail]; If (cansendmail) {//create Mail View Controller MFmailcomposeviewcontroller *MAILVC = [[mfmailcomposeviewcontroller alloc]init]; Set to receive mail, array, can achieve mass [MAILVC settorecipients:@[@ "[email protected]", @ "[email protected]"]; Set the CC object, [MAILVC setccrecipients:@[@ "[email protected]", @ "[email protected]"]; Set up Bcc [MAILVC setbccrecipients:@[@ "[email protected]"]; Set content [MAILVC setmessagebody:@ "nice to meet you" ishtml:no]; Set Proxy mailvc.mailcomposedelegate = self; Open Mail function [self PRESENTVIEWCONTROLLER:MAILVC animated:yes completion:nil]; }else{uialertview *alert = [[uialertview alloc]initwithtitle:@ ' Prompt ' message:@ ' your device does not support mail function ' delegate:self CancelButt ontitle:@ "ok" otherbuttontitles:nil, nil]; [alert show]; } }</pre><br><p style="margin-top:0px; margin-bottom:0px; font-family:‘STHeiti Light‘"><p style="margin-top:0px; margin-bottom:0px; font-family:‘STHeiti Light‘">Methods of the Mail agent:</p></p><p style="margin-top:0px; margin-bottom:0px; font-family:‘STHeiti Light‘"><p style="margin-top:0px; margin-bottom:0px; font-family:‘STHeiti Light‘"></p></p><pre name="code" class="objc"><pre name="code" class="objc">-(void) mailcomposecontroller: (mfmailcomposeviewcontroller *) Controller didfinishwithresult: (mfmailcomposeresult) Result error: (nserror *) error{ // mfmailcomposeresultcancelled, Cancel Send // mfmailcomposeresultsaved, Save // mfmailcomposeresultsent, send // mfmailcomposeresultfailed send failed switch (result) {case mfmailcomposeresultcancelled: NSLog (@ " Cancel Send "); break; Case Mfmailcomposeresultsaved: NSLog (@ "save"); break; Case Mfmailcomposeresultsent: NSLog (@ "send success"); break; Case Mfmailcomposeresultfailed: NSLog (@ "failed"); break; Default: Break ; } [self Dismissviewcontrolleranimated:yes completion:nil]; }</pre></pre><br>Final Effect: (because the simulator can not be shown to send text messages, so the following phenomenon Occurs)<p><p></p></p><br><p style="margin-top:0px; margin-bottom:0px; font-family:‘STHeiti Light‘"><p style="margin-top:0px; margin-bottom:0px; font-family:‘STHeiti Light‘"><span style="color:#ff0000"><span style="line-height:26px; font-family:‘STHeiti Light‘">Daily Update Attention</span> <span style="line-height:26px; font-family:‘STHeiti Light‘">:</span>http://weibo.com/hanjunqiang<span style="line-height:26px; font-family:‘STHeiti Light‘"> Sina Weibo</span><br></span></p></p><br><br><p style="margin-top:0px; margin-bottom:0px"><p style="margin-top:0px; margin-bottom:0px"><span style="font-family:Microsoft YaHei; font-size:18px; color:#ff0000"><br></span></p></p><p><p>Send Sms/email implementation in iOS Han Junqiang blog</p></p></span>
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.