我想做一个标签,点击它可以拨打电话。我知道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)
}
}
类型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)
}
}
}
关于ios - 单击Swift中的标签,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/31375688/