简体   繁体   English

使用Swift拨打电话

[英]Calling a phone number with Swift

How can I assign a variable that stores a phone number to a button to call that number? 如何将存储电话号码的变量分配给按钮以呼叫该号码?

I am using Swift. 我正在使用Swift。 Here is my code: 这是我的代码:

func base() {

    var myBase = sharedDefaults!.objectForKey("base") as? String

    if myBase != nil {
        if myBase == "FRA" {
            titleLabel.text = "FRANKFURT"
            adressLabel.text = "some adress"
            var phone = "+49123456 69 69804616"
            coorPhone.setTitle("Duty Desk : \(phone)", forState: .Normal)
            logoImage.image = UIImage(named: "flaggermany")

        } else if myBase == "LHR" {
            titleLabel.text = "LONDON"
            adressLabel.text = "some adress"
            var phone = "+44123456"

            coorPhone.setTitle("Duty Desk : \(phone)", forState: .Normal)

            logoImage.image = UIImage(named: "flaguk")

        }
    }

    @IBAction func phoneButtonPressed(sender: AnyObject) {
        // let telUrlString = "tel://" + phone
        // UIApplication.sharedApplication().openURL(NSURL(string: "\      (telUrlString)")!)
    }
@IBAction func phoneButtonPressed(sender: UIButton) {
    if let url = NSURL(string: "tel://\(phone)") {
        UIApplication.sharedApplication().openURL(url)
    }
}

you can use this code if you define the variable phone at the top of your viewController.swift file (below the class call), that way it can be accessed in the function. 如果您在viewController.swift文件顶部(在类调用下方)定义了变量phone,则可以使用此代码,这样就可以在函数中对其进行访问。

You can declare your phone variable as a global for your class by declaring it at the below of your class declaration as shown below: 您可以通过在类声明的下面进行声明,将您的phone变量声明为类的全局变量,如下所示:

class ViewController: UIViewController {

     let sharedDefaults = NSUserDefaults.standardUserDefaults()
     var phone = ""

}

By declaring this way you can access it anywhere in your class and now you can assign value to it this way in your function: 通过声明这种方式,您可以在类中的任何位置访问它,现在您可以在函数中通过这种方式为其赋值:

func base() {

    var myBase = sharedDefaults.objectForKey("base") as? String

    if myBase != nil {
        if myBase == "FRA" {

            titleLabel.text = "FRANKFURT"
            adressLabel.text = "some adress"
            phone = "+49123456 69 69804616"
            coorPhone.setTitle("Duty Desk : \(phone)", forState: .Normal)
            logoImage.image = UIImage(named: "flaggermany")


        }
        else if myBase == "LHR" {
            titleLabel.text = "LONDON"
            adressLabel.text = "some adress"
            phone = "+44123456"
            coorPhone.setTitle("Duty Desk : \(phone)", forState: .Normal)
            logoImage.image = UIImage(named: "flaguk")

        }
    }
}

After that you can use it this way into another function: 之后,您可以通过这种方式将其用于另一个函数:

@IBAction func phoneButtonPressed(sender: AnyObject) {

    if let url = NSURL(string: "tel://\(phone)") {
        UIApplication.sharedApplication().openURL(url)
    }
}

Hope this will help you. 希望这会帮助你。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM