Call using an app when uibutton is clicked - ios

I have an app that displays a telephone number, and that number is in a uibutton. When the button is clicked, I want it to call. What is the best way to do it?

Get the text from the button into an NSUrl and pass it in:
let url = NSURL(string: button.currentTitle)
UIApplication.sharedApplication().openURL(url)

If when your selector for the button is called it passes the button into the method. So you can just grab it from there.
func buttonTapped(sender: AnyObject) {
if let button : UIButton = sender as? UIButton {
if let url = NSURL(string: button.titleLabel?.text) {
UIApplication.sharedApplication().openURL(url)
}
}
}

Related

Get id of button from webview on tap in swift

I have a webview in which I load a url coming from a backend server. There is a button in my webview on which when user tap I want to get the id of that button. How I can get the id of that button? I have used a tap gesture on which when I click I'm trying to get the id but it's not working, my code is this,
let webViewTapped = UITapGestureRecognizer(target: self, action: #selector(self.tapAction(_:)))
webViewTapped.numberOfTapsRequired = 1
webViewTapped.delegate = self
webView.addGestureRecognizer(webViewTapped)
#objc func tapAction(_ sender: UITapGestureRecognizer?) {
print("touched")
// Get the specific point that was touched
let point: CGPoint? = sender?.location(in: view)
webView.evaluateJavaScript("document.getElementById(\"hdfUserId\").value") {(response, error) in
if (response != nil) {
let title = response as! String
print(title)
}
// error handling
}
}
This is how I'm trying to get the button id by javascript, but I'm getting fail in that

Button in Today Widget Xcode

New here to Xcode and Swift. I am working on an IOT app that enables me to switch a light on at a distance through a button on my today extension.
So I activated app groups and I am using UserDefaults to share info between app and widget. My logic being when button is pressed on the widget it returns a bool true. And if it returns true, I activate for now what is a LED on my ESP8266.
However, this doesn't seem to work, when I click the button nothing happens. What am I doing wrong? Any help or hints would be great! Thank you.
In viewcontroller.swift
override func viewDidLoad() {
defaults?.synchronize()
let Distribute = defaults?.bool(forKey: "Distribute")
if Distribute == true {
let requestURL = NSURL(string: led1on_URL)
// Instantiate an NSURLRequest object using the
// requestURL NSURL
let request = NSURLRequest(url: requestURL! as URL)
// Use the webview to send the request to the
// request NSURLRequest
View1.loadRequest(request as URLRequest)
super.viewDidLoad()
#IBAction func LED_control(_ sender: UIButton) {
}
}
In todayviewcontroller I have:
#IBAction func ButtonSnd(_ sender: Any) {
defaults?.set(true, forKey: "Distribute") //Bool Data Type
//Pass anything with this line
//defaults?.set("\(aNumber)", forKey: "userKey")
defaults?.synchronize()
print("Buttonpressed")
}
}

Hyperlinking a button

I am trying to create a button that goes to a link when pressed using Swift 3. When I run the app and click the button, nothing happens. Here is the code for the button's #IBAction:
#IBAction func facebookButton(_ sender: AnyObject) {
if let url = NSURL(string: "http://www.facebook.com/MatchWear-1638094356470603/info?tab=overview"){
UIApplication.shared.open(url as URL, options: [:], completionHandler: nil)
}
}
Assuming that you are pretty sure that the #IBAction is connected to your button, it should looks like this:
if let url = URL(string: "http://www.facebook.com/MatchWear-1638094356470603/info?tab=overview") , UIApplication.shared.canOpenURL(url) {
UIApplication.shared.openURL(url)
} else {
// invalid URL...
}
Hope this helped.

Open URL from UIButton in CollectionViewCell

I have a UIButton in my UICollectionViewCell and it's getting data from JSON. Now I need to open a URL from each button (each button have a different url that also comes from JSON).
I managed to open the URL with:
let weburl = "http://example.com"
UIApplication.shared.openURL(URL(string: weburl)!)
But now I need to kinda pass an url to each button. Any ideas of how can i achieve this?
You can have an array of urls:
let urls = [url1, url2, ...]
And then assign the tag property of each button to the index of its corresponding url. Now you can easily manage what you want:
#IBAction func handleTouch(_ sender: UIButton) {
// assumes that the buttons' tags start at 0, which isn't a good idea.
// see #rmaddy comment bellow
let url = urls[sender.tag]
// use the version of the open method shown bellow because the other one becomes deprecated in iOS 10
UIApplication.shared.open(URL(string: url)!, options: [:], completionHandler: nil)
}
EDIT
Other solution would be to just store the url in the cell itself, and in the button handler open the url corresponding to its cell.
FYI openURL is deprecated in iOS 10. I suggest the following if you need to support older versions of ios:
let url = URL(string: "alexa://")!
if #available(iOS 10, *) {
UIApplication.shared.open(url, options: [:], completionHandler: {
(success) in
guard success else {
//Error here
}
//Success here
})
} else {
if let success = UIApplication.shared.openURL(url) {
//Success here
} else {
//Error here
}
}
Otherwise just use UIApplication.shared.open. Also I would add a URL field to the data model you are passing to your tableViewCell and just look up the URL from the model.

UITapGestureRecognizer is not opening url

I have created an action when tapping an image to open an URL in navigator but when I tap on it, nothing opens up. This is my code:
#IBAction func Anuncio(sender: UITapGestureRecognizer) {
if let url = NSURL(string: "http://capturando.es/") {
UIApplication.sharedApplication().openURL(url)
}
}
check once yourImagename.userInteractionEnabled = true or false if it is false just enable once thereafter all other functions are automatically triggered
yourImagename.userInteractionEnabled = true

Resources