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

问题描述:

我有一个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 = "[email protected]" 

    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) 
    } 

} 

如果将这些“标签”替换为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://[email protected]")! 
    UIApplication.shared.open(emailURL, options: [:], completionHandler: nil) 
} 
+0

任何想法在我的代码,其中代码将取代它? –