I have a Swift app that I'm working on and on the contact us page, there's a bit that shows "Facebook Profile", Telephone Number, Email Address.
I have been told there's a way to have the code set up so when someone clicks on, for example the telephone number, it will open in iPhone phone dialer.
Any ideas where to find the code?
I've been sent this from someone who says they know the code, but when I asked him where the code goes, he said I just have to fiddle.
This is his code:
You need to add this code for each label you need to add for it an action in 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 = "foo@bar.com"
let url = NSURL(string: "mailto:\(email)")
UIApplication.sharedApplication().openURL(url)
}
And this is my code for the contact page (contactViewController.swift). Can someone have a look for me and help me out?
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)
}
}