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).
Topic:
UI Frameworks
SubTopic:
SwiftUI
Tags: