IOS development calls the system call and text messaging interface and sends text messages in the program, while ios sends text messages

Source: Internet
Author: User

IOS development calls the system call and text messaging interface and sends text messages in the program, while ios sends text messages

At the beginning of this blog, let's talk about the reasons for writing this blog. At present, I am working on a small project and want to use the verification code in this application to send it to other users. I don't think much about the specific details of how to send text messages in the application, so I just thought about Baidu, I also found some blogs about this. I clicked in and looked at it. I felt a little disappointed and wrote too little detail. I just listed the simple code and didn't comment out the code, probably because it is too simple. Today, after finishing the project's text message function, I feel it is necessary to sort out this part of content. Is it good to make a souvenir. Let's talk a little bit about what is going on today. The text message below, of course, requires a real machine test to call.

1. Call system functions

In iOS, the call and text message functions of the system are relatively simple. Previously, I also mentioned this in SSO. In iOS, one application can open another application and only one line of code is required, the same is true for calling, sending text messages, sending emails, and opening webpages, but different protocols are used.

1. Call

This is relatively simple. The following is the code for making a call: The content of % @ is the mobile phone number self. myApplication is the Application Singleton.

2. Open

The above method is to open the corresponding application and then do the corresponding thing, so how can we send text messages to our users in this application? Let's implement this function.

1. Introduce the framework we want to use (in fact, sending text messages in the application is very similar to ImagePickerViewController, there is no difficulty), we need to introduce MessageUI. framework


3. The following is the core code for sending text messages.

(1) the code used to determine whether a device has the function of sending text messages is as follows:

1 / / method of sending SMS
2 -(void)sendMessage
3 {
4 / / it is used to judge whether there is the function of sending SMS (there is no SMS function on the simulator)
5     Class messageClass = (NSClassFromString(@"MFMessageComposeViewController"));
Six
7 / / judge whether there is SMS function
8     if (messageClass != nil) {
9 / / there is something to do with the send function
10}
11 else
12 {
Thirteen
14 UIAlertView * alterview = [[UIAlertView alloc] initwithtitle: @ "prompt" message: @ "IOS version is too low (after IOS 4.0)" delegate: Nil cancelbuttontitle: @ "Cancel" other buttontiles: Nil];
Fifteen
16         [alterView show];
17}
Eighteen
Nineteen
20}


(2). If you have the SMS sending function, you must determine that the iOS version supports "MFMessageComposeViewController". iOS4.0 and later.

1 / / with SMS function
2         if ([messageClass canSendText]) {
3 / / send SMS
4}
5         else
6 {
7 UIAlertView * alterview = [[UIAlertView alloc] initwithtitle: @ "prompt" message: @ "the device does not have the function of sending SMS ~" delegate: Nil cancelbuttontitle: @ "Cancel" other buttontiles: Nil];
Eight
9             [alterView show];
10}
Eleven

 

(3) After various verifications, confirm that the device can use MFMessageComposeViewController, and we will start to use it.

1 / / instantiate mfmessagecomposeviewcontroller and set delegation
2             MFMessageComposeViewController *messageController = [[MFMessageComposeViewController alloc] init];
3             messageController.delegate = self;
Four
Five
6 / / splicing and setting SMS content
7 nsstring * MessageContent = [nsstring stringwithformat: @ "honey, this is the invitation code for you and me:% @, self.authcodelabel. Text];
8             messageController.body = messageContent;
Nine
10 / / set who to send
11             messageController.recipients = @[self.phoneNumberTextField.text];
Twelve
13 / / push to send attempt controller
14             [self presentViewController:messageController animated:YES completion:^{
Fifteen
16             }];

 

(4) I almost forgot to implement the Commission callback protocol ~ The MFMessageComposeViewControllerDelegate and UINavigationControllerDelegate protocols must be implemented. 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 = @ "sent SMS";
8             break;
Nine
10         case MessageComposeResultFailed:
11 tipcontent = @ "failed to send SMS";
12             break;
Thirteen
14         case MessageComposeResultSent:
15 tipcontent = @ "sent successfully";
16             break;
Seventeen
18         default:
19             break;
20}
Twenty-one
22 UIAlertView * alterview = [[UIAlertView alloc] initwithtitle: @ "prompt" message: tipcontent delegate: Nil cancelbuttontitle: @ "Cancel" otherbuttontiles: Nil];
23     [alterView show];
24} 

 

Now, you can send text messages. This blog is here first ~


C # Develop interfaces for free text message sending that can be called

Currently, the free APIs are basically no longer available.
I am using the Internet-era sms interface. If you are interested, take a look at it and ask for money.
The interface is a ready-made call, which is very convenient.

Why does the android app fail to send messages by directly calling the system SMS interface? Can be sent normally on the simulator

Many of modules are not native and have been modified. Therefore, common method calls may fail, just as camera calls are somewhat different from general mobile phones. We hope to help you. If you have any questions, we can continue to communicate with you.

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.