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