How to give Call and Copy to clipboard options
Posted By : Aditya Kumar Sharma | 17-May-2016
Some time we want to make a call or copy the Phone number form someone's profile page. So that can be done by following these steps. For making Call and Copy to clipboard option we need to show UIAlertViewController like this:
for making the UIAlertController we need the do following code :
func showActionSheet(index:Int){ let optionMenu = UIAlertController(title: nil, message: "Choose Option", preferredStyle: .ActionSheet) let callAction = UIAlertAction(title: "Call", style: .Default, handler: { (alert: UIAlertAction) -> Void in self.callPhone(self.userNumber) }) let copyToClipAction = UIAlertAction(title: "Copy to clipboard", style: .Default, handler: { (alert: UIAlertAction) -> Void in self.copyToClipboard(self.userNumber) }) let cancelAction = UIAlertAction(title: "Cancel", style: UIAlertActionStyle.Cancel) { (action) in // ... } optionMenu.addAction(callAction) optionMenu.addAction(copyToClipAction) optionMenu.addAction(cancelAction) self.presentViewController(optionMenu, animated: true, completion: nil) }
In this code firstly we create CallAction which shows call option where func is called with parameter (Phone number as parameter) - self.callPhone(self.userNumber)
then we have created copyToClipAction which shows the option to copy the number in the clipboard - self.copyToClipboard(self.userNumber)
func callPhone(number: String ){ let num = "+\(number)" UIApplication.sharedApplication().openURL(NSURL(string: "tel://\(num)")!) }
In callPhone() func we call openUrl with "tel://" which opens the Phone call page giving option to make call.
func copyToClipboard(number: String) { UIPasteboard.generalPasteboard().string = "+\(number)" }
In copyToClipboard() func we have the UIPasteBoard func which copy the parameter is been passed in and can be paste anywhere in the phone.
Now enjoy your code.
Thanks
Cookies are important to the proper functioning of a site. To improve your experience, we use cookies to remember log-in details and provide secure log-in, collect statistics to optimize site functionality, and deliver content tailored to your interests. Click Agree and Proceed to accept cookies and go directly to the site or click on View Cookie Settings to see detailed descriptions of the types of cookies and choose whether to accept certain cookies while on the site.
About Author
Aditya Kumar Sharma
Aditya is a bright iOS developer, have knowledge of objective C, swift, swift 3, JSON, Core data and iPhone development. Apart from that he loves to travel and explore new things.