On Tap UILabel (gesture recognizer) finds zero in the cell of the prototype tableview for one cell and its working tone for the other two cells

I am trying to implement a UITapGestureRecognizer, the idea is that when I click a tag, I get a pop-up message with a number to call, and it should appear with a pop-up warning saying about a call or cancellation.

The code I wrote worked for me in 3-4 places, but I got stuck in one place On this screen I have a table view with the prototype cells grouped here, please check this image:

Image link

Third cell

Now, if I press 065668982 , I have canOpenURL: failed URL: "telprompt: // 065668982" - error: "This application is not allowed to request the telprompt scheme" , which actually works on the iPhone not on the simulator, and it pulls to make a call that works fine.

Second cell

If I press 065454858 , I have canOpenURL: failed URL: "telprompt: // 065668982" - error: "This application is not allowed to request the telprompt scheme" strong>, which actually works on the iPhone not on the simulator, and it pulls on a call which works fine.

first cell

But for the first one, it never works and ends with a fatal error: unexpectedly found nil when unpacking an optional value

NOTE. I get the phone number from the API and add the data controller to the UITableViewCell controller.

I hope that makes sense, Thanks in advance for any help, also, if I do not understand, please comment below

Here is my code:

import UIKit import Foundation class XyzTableViewCell: UITableViewCell { @IBOutlet weak var phoneNumber: UILabel! var touchContact : String = "" var myCell: MyCellData! { didSet { self.updateUI() } } func updateUI() { touchContact = vicarCell.phone_no //Tap Gesture tapGestureAddonView() } //MARK:- Tap to Call and Open Email func tapGestureAddonView(){ let contacttap = UITapGestureRecognizer(target: self, action:("contactTapped")) contacttap.numberOfTapsRequired = 1 phoneNumber!.userInteractionEnabled = true phoneNumber!.addGestureRecognizer(contacttap) } func contactTapped() { // do something cool here print("contactTapped") print(touchContact) dispatch_async(dispatch_get_main_queue()) { if UIApplication.sharedApplication().canOpenURL(NSURL(string: "telprompt://\(self.touchContact)")!){ UIApplication.sharedApplication().openURL(NSURL(string: "telprompt://\(self.touchContact)")!) } else{ //showAlert("Info",message: "Your device could not called" ,owner: self) } } } 
+5
source share
1 answer

Problems: 1) you should add a gesture only once 2) you should check NSURL for nil. Let me assume that you are using a storyboard and slightly improving your code.

 class XyzTableViewCell: UITableViewCell { @IBOutlet weak var phoneNumber: UILabel! var touchContact : String = "" var myCell: MyCellData! {didSet {self.updateUI()}} func updateUI() { touchContact = myCell.phone_no // did you mean myCell instead vicarCell? } override func awakeFromNib() { super.awakeFromNib() tapGestureAddonView() } func tapGestureAddonView(){ let contacttap = UITapGestureRecognizer(target: self, action: #selector(contactTapped)) contacttap.numberOfTapsRequired = 1 phoneNumber!.userInteractionEnabled = true phoneNumber!.addGestureRecognizer(contacttap) } func contactTapped() { print("contactTapped") print(touchContact) if touchContact.isEmpty {return} guard let url = NSURL(string: "telprompt://\(touchContact)") else { print("url string invalid") return } dispatch_async(dispatch_get_main_queue()) { if UIApplication.sharedApplication().canOpenURL(url){ UIApplication.sharedApplication().openURL(url) } else{ //showAlert("Info",message: "Your device could not called" ,owner: self) } } } } 
+1
source

All Articles