Let's talk about how our app can access the SMS and phone functions of the system today. First of all, the text messaging function, relatively simple, with the previous approach is not very different, to use uiapplication, it is a single case. Our function is to click on a button and then dial a built-in phone, you need to add the following statement to the button's action:
@IBAction func phonebtn (Sender:uibutton) { //var url1 = Nsurl (string: "tel://" +canguanarray[0].tel) var url1 = Nsurl (string: "tel://10086") uiapplication.sharedapplication (). OpenURL (url1!) }
The TEL keyword represents the phone, similar to what was done on the previous OC, and if the call to be dialed is a value obtained, refer to the notation in the note. In addition to the Tel keyword, there are SMS keywords:
var url1 = Nsurl (string: "sms://10086")
In this case, we open a 10086 SMS interface, if we want to open a browser interface, use the following code:
var url1 = Nsurl (string: "http://blog.csdn.net/cg1991130")
However, using this method to send text messages can not set the content of text messages, can only set the recipient. If we want to customize the content of text messages, use the following method:
First import the header file in VC:
Import Messageui
Then let the VC inherit Mfmessagecompose agent:
Func Cansendtext (), bool{ return Mfmessagecomposeviewcontroller.cansendtext () }//is used to indicate whether a message can be sent from the user func 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:
Swift UI special training 40 using Swift to make calls and texting