Today's work encountered the need to send text (iOS) function, and then conveniently recorded a bit

Source: Internet
Author: User

There are two ways to send text messages in iOS

1. System SMS for Out of program

2. Call system within the program to send text messages

The first is simple, call the URL directly

The code under OC is

1 [[uiapplication sharedapplication] Openurl:[nsurl urlwithstring:[nsstring stringWithFormat:@ " sms:%@ "@"13888888888"]]
OC Version

The code under Swift

Uiapplication.sharedapplication (). OpenURL (Nsurl (string: "sms:13888888888")!)

The second Kind

The code under OC

1) Import the messageui.framework and introduce the header file:

2) Implement Proxy method Mfmessagecomposeviewcontrollerdelegate
-(void) Messagecomposeviewcontroller: (Mfmessagecomposeviewcontroller *Controller Didfinishwithresult: (messagecomposeresult) result{[self dismissviewcontrolleranimated:yes    Completion:nil]; Switch(Result) { Casemessagecomposeresultsent://Message Delivery Success             Break;  Casemessagecomposeresultfailed://Message Delivery failed             Break;  Casemessagecomposeresultcancelled://message canceled by user             Break; default:             Break; }}
View Code

3) Send SMS
-(void) Showmessageview: (Nsarray *) phones title: (NSString *) title body: (NSString *) body{if([Mfmessagecomposeviewcontroller Cansendtext]) {Mfmessagecomposeviewcontroller* Controller =[[Mfmessagecomposeviewcontroller alloc] init]; Controller.recipients=phones; Controller.navigationBar.tintColor=[Uicolor Redcolor]; Controller.body=body; Controller.messagecomposedelegate=Self ;        [Self Presentviewcontroller:controller animated:yes completion:nil]; [[[Controller viewcontrollers] lastobject] navigationitem] settitle:title];//Modify the text message interface title    }    Else{Uialertview*alert =[[Uialertview alloc] initwithtitle:@ prompt message message:@ The device does not support SMS features Delegate: nil cancelbuttontitle:@ ok o        Therbuttontitles:nil, nil];    [Alert show]; }}
View Code

Parameter phones: An array of mobile phone numbers for texting, in which the array is a single-shot, multiple-mass.

4) Call the method of sending text messages
[Self showmessageview:[nsarray arraywithobjects:@13888888888, @13999999999, nil] Title: @test Body: @ Are you local tyrants, huh?];
View Code

Swift version

Import Messageui


Then let the VC inherit Mfmessagecompose agent:

Class caipindetailviewcontroller:uiviewcontroller,mfmessagecomposeviewcontrollerdelegate func CanSendText () bool{    return Mfmessagecomposeviewcontroller.cansendtext ()    }//is used to indicate whether a message can send a func from the user    Configuredmessagecomposeviewcontroller (), mfmessagecomposeviewcontroller{let    MESSAGECOMPOSEVC = Mfmessagecomposeviewcontroller ()        messagecomposevc.messagecomposedelegate = self        messagecomposevc.body = " Hi! \ (caipinarray[0].rest) \ (caipinarray[0].name) taste very good, invite you to share-invitation from Sofun "        return MESSAGECOMPOSEVC            }     func Messagecomposeviewcontroller (controller:mfmessagecomposeviewcontroller!, didfinishwithresult result: Messagecomposeresult) {        controller.dismissviewcontrolleranimated (true, Completion:nil)    }


Then add the following code to the action method of the button:

@IBAction func Share (Sender:uibutton) {let        Shareview = Shareviewcontroller ()        Self.presentviewcontroller ( Shareview, Animated:true, Completion:nil)    }    @IBAction func message (Sender:uibutton) {        if Self.cansendtext () {let        Messagevc = Self.configuredmessagecomposeviewcontroller ()          Presentviewcontroller ( MESSAGEVC, Animated:true, Completion:nil)        } else {let        Erroralert = Uialertview (title: "Cannot send", message: "Your device is not short Function ", Delegate:self, Cancelbuttontitle:" Cancel ")        }            }

Let's test it on the real machine:






Today's work encountered the need to send text (iOS) function, and then conveniently recorded a bit

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.