Swift UI special training 40 using Swift to make calls and texting

Source: Internet
Author: User

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

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.