iOS development call system call to send SMS interface and text message within the program

Source: Internet
Author: User

At the beginning of this blog, let's talk about the reasons for writing a blog. At present, to do a small project, to use in the application to send a verification code to other users, how in the application sent the details of the text message is not big up, so Baidu a bit, found there is also about this aspect of the blog, point into the look, personal feel a little disappointed, write too not detailed, just simple code listing, And the code is not annotated, presumably because it's too simple. Today after the completion of the project texting function feel the need to put this part of the content, do a souvenir is also good is not it. Talk less, cut to the chase today. The following texting, call of course need to test the real machine.

First, call the system function

In iOS to open the system itself to call and text messaging function is relatively simple, before in the SSO also raised a mouth, iOS can be opened by an app another app, just a line of code, call, send text messages, email, open Web page is the same, just use the same protocol.

1. Call

This is still relatively simple, the following is the phone code:%@ is the mobile phone number self.myapplication is a singleton application.

2. Send SMS

Sending a text message is just a different protocol from the phone. You can make a call by changing the protocol.

3. Send Email

It's a change of protocol.

    

4. Open the URL

Second, open in this application

The above method is to open the appropriate application, and then do the corresponding things, then how can we in this application to send our users SMS? Let's implement this feature below.

1. Introduction of the framework we want to use (in fact, the use of sending SMS and Imagepickerviewcontroller in the application is very similar, there is no difficult place), we need to introduce messageui.framework, into

2. Import the appropriate header file in the PCH file

3. Below is the core code for sending SMS

(1) To determine whether the device has to send SMS function code as follows:

1//method of sending SMS 2-(void) SendMessage 3 {4     //used to determine if there is a function to send SMS (there is no SMS function on the simulator) 5     Class MessageClass = (nsclassfromstring (@ "Mfmessagecomposeviewcontroller")); 6      7     //Determine if there is a SMS function 8     if (messageclass! = nil) {9           //have send function to do things     }11     else12     {         14          Uialertview *alterview = [[Uialertview alloc] initwithtitle:@ "hint" message:@ "iOS version too Low (iOS4.0 later)" Delegate:nil cancelbuttontitle:@ "Cancel" otherbuttontitles:nil];15         [alterview show];17     }18     20}

  

(2). If you have a text messaging function, you have to decide that iOS release supports "Mfmessagecomposeviewcontroller". Support After iOS4.0

1     //SMS Function 2         if ([MessageClass Cansendtext]) {3               //SMS 4         } 5         Else 6         {7             Uialertview * Alterview = [[Uialertview alloc] initwithtitle:@ "Prompt" message:@ "The device does not send SMS function ~" Delegate:nil cancelbuttontitle:@ "Cancel" Otherbuttontitles:nil]; 8              9             [Alterview show];10         }11     

(3), after various verification to determine the equipment can use Mfmessagecomposeviewcontroller, we began to use the

1          //Instantiate Mfmessagecomposeviewcontroller, and set delegate 2             mfmessagecomposeviewcontroller *messagecontroller = [[ Mfmessagecomposeviewcontroller alloc] init]; 3             messagecontroller.delegate = self; 4              5              6             //stitching and setting SMS Content 7             nsstring *messagecontent = [NSString stringwithformat:@ "Dear, this is exclusive to you my application of the invitation code:%@", Self.authCodeLabel.text]; 8             messagecontroller.body = messagecontent; 9             //set to whom to send             messagecontroller.recipients = @[ SELF.PHONENUMBERTEXTFIELD.TEXT];12             //push to send attempt controller             [self Presentviewcontroller: Messagecontroller animated:yes completion:^{15             }];

(4), almost forgot, the implementation of the corresponding delegation callback protocol is indispensable ~ to achieve the mfmessagecomposeviewcontrollerdelegate,uinavigationcontrollerdelegate of the two protocols. The callback after sending is as follows:

1//Method of callback after sending SMS 2-(void) Messagecomposeviewcontroller: (Mfmessagecomposeviewcontroller *) controller Didfinishwithresult: (Messagecomposeresult) result 3 {4     nsstring *tipcontent; 5     switch (result) {6         case Messagecomposeresultcancelled:7             tipcontent = @ "Send SMS"; 8 break             ; 9 Case         Messagecomposeresultfailed:11             tipcontent = @ "Send SMS Failed";             break;13 case         messagecomposeresultsent:15             tipcontent = @ "sent successfully";             break;17         default:19             break;20     }21     Uialertview *alterview = [[Uialertview alloc] initwithtitle:@ "hint" message:tipcontent delegate: Nil cancelbuttontitle:@ "Cancel" otherbuttontitles:nil];23     [Alterview show];24}

Here you can send a text message, this blog first to come here ~

iOS development call system call to send SMS interface and text message within the program

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.