Search code examples
swiftxcodebuttoncopy-pastestreet-address

Enable copy button title on long press on the button


I have UIButton for an address in my tableview cell. When I tap on it once; I open google map with the direction no problem. Now, I want to provide the option for long gesture so if you hold your finger on the button, it provides the option to copy the address which is in the title of the button. This is my code:

@IBOutlet weak var addressBtn: UIButton!


override func awakeFromNib() {
    super.awakeFromNib()

    addLongPressGesture()
}

@objc func longPress(gesture: UILongPressGestureRecognizer) {
    if gesture.state == UIGestureRecognizer.State.began {

        // how do I make it possible to copy the title of the button here? The address is already inserted as the title of the button

    }
}

func addLongPressGesture(){
    let longPress = UILongPressGestureRecognizer(target: self, action: #selector(longPress(gesture:)))
    longPress.minimumPressDuration = 0.5
    self.addressBtn.addGestureRecognizer(longPress)
}

This is where on one tap it goes to the map with no problem; so I have no issue here but just fyi: @IBAction func addressClicked(_ sender: Any) {

    if (UIApplication.shared.canOpenURL(NSURL(string:"comgooglemaps://")! as URL)) {

        let street = order.street.replacingOccurrences(of: " ", with: "+")
        let postalCode = order.postalCode.replacingOccurrences(of: " ", with: "+")

        if street == "" || order.city == "" || order.province == "" || postalCode == ""{
            UIApplication.shared.open(URL(string:"comgooglemaps://?saddr=&daddr=\(order.longitude),\(order.latitude)&directionsmode=driving")! as URL)
        } else {
            UIApplication.shared.open(URL(string:"comgooglemaps://?saddr=&daddr=+\(street),+\(order.city),+\(order.province),+\(postalCode)&directionsmode=driving")! as URL)
        }

        } else {
            NSLog("Can't use comgooglemaps://")
        }
    }

Solution

  • I figured it out with the following code:

    //Create the AlertController and add Its action like button in Actionsheet
            let actionSheetControllerIOS8: UIAlertController = UIAlertController(title: nil, message: nil, preferredStyle: .actionSheet)
    
        actionSheetControllerIOS8.view.tintColor = AppColors.Blue
    
        let cancelActionButton = UIAlertAction(title: "Cancel", style: .cancel) { _ in
        }
        actionSheetControllerIOS8.addAction(cancelActionButton)
    
        let saveActionButton = UIAlertAction(title: "Open Google Map", style: .default)
        { _ in
    
            if (UIApplication.shared.canOpenURL(NSURL(string:"comgooglemaps://")! as URL)) {
    
                let street = order.street.replacingOccurrences(of: " ", with: "+")
                let postalCode = order.postalCode.replacingOccurrences(of: " ", with: "+")
    
                if street == "" || order.city == "" || order.province == "" || postalCode == ""{
                    UIApplication.shared.open(URL(string:"comgooglemaps://?saddr=&daddr=\(order.longitude),\(order.latitude)&directionsmode=driving")! as URL)
                } else {
                    UIApplication.shared.open(URL(string:"comgooglemaps://?saddr=&daddr=+\(street),+\(order.city),+\(order.province),+\(postalCode)&directionsmode=driving")! as URL)
                }
    
            } else {
                NSLog("Can't use comgooglemaps://")
            }
        }
        actionSheetControllerIOS8.addAction(saveActionButton)
    
        let deleteActionButton = UIAlertAction(title: "Copy Address", style: .default)
        { _ in
    
            let address = "\(order.street), \(order.city), \(order.province), \(order.postalCode)"
            let pasteBoard = UIPasteboard.general
            pasteBoard.string = address
    
        }
        actionSheetControllerIOS8.addAction(deleteActionButton)
        self.present(actionSheetControllerIOS8, animated: true, completion: nil)
        }