2015-07-13 57 views
1

我想製作點擊它以打電話給號碼的標籤。我知道iOS有這個選項,但我怎麼能在Swift中做到這一點?點擊Swift中的一個標籤

我發現只是如何做到這一點在ObjC:

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

誰能幫助我呢?

+0

的可能重複[如何使用的OpenURL一個電話與SWIFT(HTTP://計算器。 com/questions/24251259/how-to-use-openurl-for-a-phone-call-with-swift) – ozgur

回答

4
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) 
    } 
} 
} 
+0

我可以將它連接到標籤嗎?標籤沒有點擊功能。那麼,我怎麼能意識到它呢? –

+0

檢查更新後的答案兄弟,在標籤的地方使用自定義的UIButton,這很容易上班 –

+0

如果您需要幫助請問我,我希望與您同行\ –