Load link from tab bar item

is it possible to load a link such as

Link("Apple", destination: URL(string: "https://www.apple.com")!)

from a tab bar item? if so how?

struct MainView: View {

  

  @State private var selectedTab = "Three"

  

    var body: some View {

        TabView(selection: $selectedTab) {



          LiveView()

              .tabItem {

                  Label("LIVE", systemImage: "camera.metering.center.weighted")

                  .font(Font.title.weight(.ultraLight))

              }

              .tag("One")

if I add an link or text I get errors. I appreciate any help!

if one adds         Text("Test")

            .tabItem{

            }

            .tag("Ten")

one gets the error "Extra argument in call" which makes no sense

https://developer.apple.com/documentation/swiftui/openurlaction

works but loads a view with a text link not opens safari

You can detect when the user taps on the specified tab with this:

// add to the TabView
.onChange(of: selectedTab) { [selectedTab] newTab in
    if newTab == "OpenLinkTab" {
        self.selectedTab = selectedTab // return to previously selected tab

        // open URL here
        let url = URL(string: "https://www.apple.com")!
        openURL(url)
    }
}

When the tab item is tapped, it will look like a regular button press as the tab view switches back to the previously selected tab. This is when you can open the link providing the behaviour you want.

This solution would be similar to implementing the UITabBarControllerDelegate.tabBarController(_:shouldSelect:) delegate method and returning false (in a UIKit app).

Load link from tab bar item
 
 
Q