0

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?

rmaddy
  • 314,917
  • 42
  • 532
  • 579
  • You need to use delegates. Check out this post: http://stackoverflow.com/questions/24099230/delegates-in-swift – Eeshwar Nov 02 '16 at 17:23

2 Answers2

1

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.

Reynaldo Aguilar
  • 1,906
  • 1
  • 15
  • 29
  • 1
    A few things to consider. This assumes you set each button's tag from 0 to n - 1 where n is the number of buttons. A tag of 0 is the default for any view with no explicit tag. It's best to avoid relying on a tag value of 0. More importantly, relying on a button's tag is risky. It fails under many circumstances such as if the collection view is dynamic (cells can be added, removed, or reordered). – rmaddy Nov 02 '16 at 17:44
  • @rmaddy Yeah, you are right. I don't like to use this property too much, but some times it helps. Thanks for the tip "A tag of 0 is the default for any view with no explicit tag. It's best to avoid relying on a tag value of 0." I really helps to avoid bugs – Reynaldo Aguilar Nov 02 '16 at 17:49
  • Another soln:why can't we make a UIButton subclass and make a property url there. and assign that with each url. – Rajan Maheshwari Nov 02 '16 at 17:53
0

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.

Josh Homann
  • 15,933
  • 3
  • 30
  • 33