Swift - open URL from button click

Viewed 32

I am learning Swift and iOS development, and I am just trying to figure out how to open an URL from a button click.

I found this answer: SwiftUI: How do I make a button open a URL in safari?

So I am trying to incorporate "Link" into my code below:

class ViewController: UIViewController {
    private let visitwebsitebutton: UIButton = {
    let visitwebsitebutton = UIButton()
    visitwebsitebutton.backgroundColor = .gray
    visitwebsitebutton.setTitle("Visit Website", for: .normal)
    visitwebsitebutton.setTitleColor(.white, for: .normal)
    visitwebsitebutton.titleLabel?.font = UIFont.boldSystemFont(ofSize: 18)
    visitwebsitebutton.layer.cornerRadius = 20
    visitwebsitebutton.Link("Some label", destination: URL(string: "https://www.mylink.com")!) // <-- link used here
    return visitwebsitebutton
  }()

  override func viewDidLoad() {
    super.viewDidLoad()

    view.addSubview(visitwebsitebutton)
  }
}

Using Link above gives me an error that reads "Value of type 'UIButton' has no member 'Link'".

What am I doing wrong and how can I fix it?

** EDIT **

I just tried this inside private let visitwebsitebutton:

visitwebsitebutton(action: {"www.redacted.com"})

But now I'm getting the below error:

Cannot call value of non-function type 'UIButton'

** EDIT 2 **

Within private let visitwebsitebutton, I attempted the following:

visitwebsitebutton.addTarget(self, action: "buttonClicked", for: UIControl.Event.touchUpInside)

Using the above, I am getting a few warning:

'self' refers to the method 'ViewController.self', which may be unexpected
Use 'ViewController.self' to silence this warning
No method declared with Objective-C selector 'buttonClicked'
Replace '"buttonClicked"' with 'Selector("buttonClicked")'

I tried to call the buttonClicked like this:

@objc func buttonClicked(sender:UIButton)
{
    if(sender.tag == 5){

        var abc = "argOne" //Do something for tag 5
    }
    print("hello")
}

And above, I am getting the below warning:

Initialization of variable 'abc' was never used; consider replacing with assignment to '_' or removing it
Replace 'var abc' with '_'

I just want to get the button to work. Please help.

1 Answers

This is how I solved the problem after hours of scavenger hunting:

class ViewController: UIViewController {
    private lazy var visitwebsitebutton: UIButton = {
    let visitwebsitebutton = UIButton()
    let mygreen = UIColor(rgb: 0x12823b)
    visitwebsitebutton.backgroundColor = mygreen
    visitwebsitebutton.setTitle("Visit Website", for: .normal)
    visitwebsitebutton.setTitleColor(.white, for: .normal)
    visitwebsitebutton.titleLabel?.font = UIFont.boldSystemFont(ofSize: 18)
    visitwebsitebutton.layer.cornerRadius = 20
    visitwebsitebutton.addTarget(self, action: #selector(visitwebsitebuttonTapped), for: .touchUpInside)
    return visitwebsitebutton
  }()

  @objc func visitwebsitebuttonTapped() {
    if let yourURL = URL(string: "https://www.somesite.com") {
            UIApplication.shared.open(yourURL, options: [:], completionHandler: nil)
    }
  }
}

If anyone needs help with iOS mobile development with Swift, and you just want to be able to click on a button and have it take you to a site, look no further.

I spent the hours hunting so you won't have to.

Related