2017-02-20 194 views
-5

我有一個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) 
    } 

} 

回答

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://[email protected]")! 
    UIApplication.shared.open(emailURL, options: [:], completionHandler: nil) 
} 
+0

任何想法在我的代碼,其中代碼將取代它? –