单击 Swift 中的标签

click on a label in Swift

我想制作一个标签,点击它可以拨打号码。我知道 iOS 有这个选项,但我怎么能在 Swift 中做到这一点?

我在 ObjC 中找到了如何做到这一点:

-(IBAction)callPhone:(id)sender {
[[UIApplication sharedApplication] openURL:[NSURL URLWithString:@"tel:2135554321"]];
}

有人可以帮我吗?

UIApplication.sharedApplication().openURL(NSURL(string: "tel://2135554321"))

例子

if let  CallURL:NSURL = NSURL(string:"tel://\(yourMobileNUmber)") {
let application:UIApplication = UIApplication.sharedApplication()
if (application.canOpenURL( CallURL)) {
  application.openURL( CallURL);
  }
else
 {
   // your number not valid
   let tapAlert = UIAlertController(title: "Alert!!!", message: "Your mobile number is invalid", preferredStyle: UIAlertControllerStyle.Alert)
tapAlert.addAction(UIAlertAction(title: "OK", style: .Destructive, handler: nil))
 self.presentViewController(tapAlert, animated: true, completion: nil)
  }
 }

Type-2

  // add gesture to your Label
 var tapGesture = UITapGestureRecognizer(target: self, action: Selector("handleTap:"))
 yourLabelName.userInteractionEnabled=true
 yourLabelName.addGestureRecognizer(tapGesture)

// handle the function of UILabel
func handleTap(sender:UITapGestureRecognizer){
     if let  CallURL:NSURL = NSURL(string:"tel://\(yourMobileNUmber)") {
let application:UIApplication = UIApplication.sharedApplication()
if (application.canOpenURL( CallURL)) {
  application.openURL( CallURL);
  }
 else
 {
   // your number not valid
   let tapAlert = UIAlertController(title: "Alert!!!", message: "Your mobile number is invalid", preferredStyle: UIAlertControllerStyle.Alert)
tapAlert.addAction(UIAlertAction(title: "OK", style: .Destructive, handler: nil))
 self.presentViewController(tapAlert, animated: true, completion: nil)
  }
 }
}