How can I pass a UILabel as a URL in Swift?

I have a UILabel called "itemOneURL" which reads a URL from an external database and adds the value to this label as a string. I've got a separate button called "viewItemOne" (using a working segue) which once selected, will take the contents of the "itemOneURL" URL and open this link using the default browser.

Here is the code I am currently using to segue the button and label.
Code Block
@IBOutlet weak var itemOneURL: UILabel!
@IBAction func viewItemOne(_ sender: UIButton) {
let firstURL = itemOneURL as? String
UIApplication.shared.open(URL(string:"\(firstURL)")! as URL, options: [:], completionHandler: nil)
}
However, once I select this button on the UI my application doesn't do anything. Whereas when I use the following syntax elsewhere, it works and loads the URL?
Code Block
UIApplication.shared.open(URL(string:"https://www.apple.com")! as URL, options: [:], completionHandler: nil)
Is there something I am doing wrong which is currently causing the button to not perform any action once selected? If so, how can I rectify this and get it working please?
Accepted Answer
with this code, firstURL is nil, because itemOneURL is a UILabel! not a String.

Thus,
URL(string:"\(firstURL)")! as URL
is nil as well

So change as:

Code Block
let firstURL = itemOneURL.text // always a String? as? String

You should also make your code more robust:

Code Block
@IBOutlet weak var itemOneURL: UILabel!
@IBAction func viewItemOne(_ sender: UIButton) {
// itemOneURL.text is always a String, but may be nil
if let firstURLString = itemOneURL.text, let url = URL(string: firstURLString) {
UIApplication.shared.open(url, options: [:], completionHandler: nil)
}
}


For the future: when you have such a problem, just add some print statements to understand what's going on.

In your initial code, if you added at line 6:
print("firstURL", firstURL)
you'd have seen immediately what the problem was.
Hi, thanks for your reply. For some reason, when I click the button nothing happens, despite re-adding the segue and adding the print("firstURL", firstURL) line - and nothing is shown. :( Any idea what it could be? Really baffling me, I even replaced the function above with a UIAlert and it didn't show either.


when I click the button nothing happens, 

Then your storyboard is broken, and the action of the button is not connected to the IBAction method.
  • Have you connected the action properly on the storyboard? No edit has made after you connected?

  • Are you sure you are instantiating the view controller through the storyboard?

 despite re-adding the segue 

Do you have a segue from the button and an IBAction ?
That cannot work. IBAction will not be called.
So you have to choose, either the segue or the IBAction.

So remove the segue and test again (check the IBAction is connected to the button: you should see a black circle at the left of @IBAction func)

And of course change the code as I told you.
How can I pass a UILabel as a URL in Swift?
 
 
Q