当用户点击号码时如何拨打电话号码?

时间:2017-02-20 23:49:43

标签: ios swift

我有一个Swift应用程序,我正在联系我们页面上工作,有一些显示" Facebook个人资料",电话号码,电子邮件地址。

我被告知有一种设置代码的方法,所以当有人点击时,例如电话号码,它将在iPhone手机拨号器中打开。

任何想法在哪里找到代码?

我是从一个说他们知道代码的人发来的,但是当我问他代码去哪里时,他说我只需要弄乱。

这是他的代码:

您需要为viewDidLoad中的操作为您需要添加的每个标签添加此代码:

yourlabel.isUserInteractionEnabled = true

let rc = UITapGestureRecognizer(target: self, action: #selector(YourViewController.yourfunction))
rc.numberOfTapsRequired = 1
rc.numberOfTouchesRequired = 1

yourlabel.addGestureRecognizer(rc)

func yourfunction(recognizer: UITapGestureRecognizer) {
    let email = "foo@bar.com"

    let url = NSURL(string: "mailto:\(email)")
    UIApplication.sharedApplication().openURL(url)
}

这是我的联系页面代码(contactViewController.swift)。有人可以帮我看看并帮帮我吗?

import UIKit

class contactViewController: UIViewController {
    @IBOutlet weak var phone: UILabel!
    @IBOutlet weak var mail: UILabel!
    @IBOutlet weak var facebook: UILabel!
    @IBOutlet weak var instagram: UILabel!
    @IBOutlet weak var address: UILabel!
    @IBOutlet weak var titleContact: UILabel!
    @IBOutlet weak var message: UILabel!
    @IBOutlet weak var scroll: UIScrollView!

    override func viewDidLoad() {
        super.viewDidLoad()

        // setup title
        self.title = "Contact us"

        // setup NavigationBar Color
        self.navigationController?.navigationBar.barTintColor = navigationBarColor

        // setup side Bar
        if(sideBarPosition == "left"){
            self.setupLeftMenuButton()
        }
        else{
            self.setupRightMenuButton()
        }

        // setup page content
        scroll.bounces = false
        phone.text = phoneNumber
        mail.text = emailAdress
        facebook.text = facebookAcount
        instagram.text = instgramAcount
        address.text = adressLocal
        titleContact.text = titleContactus

        let paragraphStyle = NSMutableParagraphStyle()
        paragraphStyle.lineSpacing = 20

        let attrString = NSMutableAttributedString(string: messageContact)
        attrString.addAttribute(NSParagraphStyleAttributeName, value:paragraphStyle, range:NSMakeRange(0, attrString.length))

        message.attributedText = attrString
        message.textAlignment = NSTextAlignment.center
    }

    // setup left menu button icon
    func setupLeftMenuButton() {
        let button = UIButton.init(type: .custom)

        button.setImage(UIImage.init(named: "menuleft.png")?.withRenderingMode(.alwaysTemplate), for: UIControlState.normal)

        button.addTarget(self, action:#selector(leftDrawerButtonPress), for: UIControlEvents.touchUpInside)

        button.frame = CGRect.init(x: 0, y: 0, width: 30, height: 17) //CGRectMake(0, 0, 30, 30)

        button.imageView?.tintColor = iconColor

        let barButton = UIBarButtonItem.init(customView: button)

        self.navigationItem.leftBarButtonItem = barButton
    }

    // setup left menu button action
    func leftDrawerButtonPress(_ sender: AnyObject?) {
        self.evo_drawerController?.toggleDrawerSide(.left, animated: true, completion: nil)
    }

    // setup right menu button icon
    func setupRightMenuButton() {
        let button = UIButton.init(type: .custom)

        button.setImage(UIImage.init(named: "menuright.png")?.withRenderingMode(.alwaysTemplate), for: UIControlState.normal)

        button.addTarget(self, action:#selector(rightDrawerButtonPress), for: UIControlEvents.touchUpInside)

        button.frame = CGRect.init(x: 0, y: 0, width: 30, height: 17) //CGRectMake(0, 0, 30, 30)

        button.imageView?.tintColor = iconColor

        let barButton = UIBarButtonItem.init(customView: button)

        self.navigationItem.rightBarButtonItem = barButton
    }

    // setup right menu button action
    func rightDrawerButtonPress(_ sender: AnyObject?) {
        self.evo_drawerController?.toggleDrawerSide(.right, animated: true, completion: nil)
    }

}

1 个答案:

答案 0 :(得分:0)

如果你更换这些"标签"它会更好。改为UIButton。不同的颜色告诉用户这些字符串是可点击的。然后,您可以将所有内容放入URL方案中,并告诉UIApplication.shared打开它们:

@IBAction func dialNumber(_ sender: Any) {
    let phoneURL = URL(string: "tel://1234567890")!
    UIApplication.shared.open(phoneURL, options: [:], completionHandler: nil)
}

@IBAction func sendEmail(_ sender : AnyObject) {
    let emailURL = URL(string: "mailto://user@domain.com")!
    UIApplication.shared.open(emailURL, options: [:], completionHandler: nil)
}