I have an existing database which I am using for my app. The database has hardcoded markdown flags for formatting the text, including URLs formatted as link text to display.
The uniqueID is to allow navigation to another view. This database is in use for both the iOS and Android version of my app so I am loathe to have separate databases to make the code easier.
As Swift already supports URLs in a Text view I am looking to figure out how to open a view using the url provided when tapped. I already have a struct which takes the uniqueID and does a database lookup for the relevant article/
struct DataView: View {
var uniqueID: String
var body: some View {
let dataToView = DataArray.shared.getDataByUniqueID(uniqueID: uniqueID)
VStack(spacing: 0) {
HeaderView(headerTitleToDisplay: dataToView.title, headerSubtitleToDisplay: dataToView.getSectionAct())
List(dataToView.dataItemEntries, id: .id) { entry in
if entry.displayInList {
DataItemCard(selectedData: entry, dataType: dataToView.dataType)
.listRowSeparator(.hidden)
}
}
.listStyle(.plain)
.padding(0)
}
// .onOpenURL { url in
// uniqueID = url.absoluteString
}
}
Appreciate that using onOpenUrl is not the best option for navigation, however given how the database is created, this seemed like the best way to handle it.;
The .onOpenURL can extract the uniqueID already, however I need to reload the DataView with the new uniqueID.
I have tried putting
.onOpenURL { url in
let newUniqueID = url.absoluteString
DataView(uniqueID: newUniqueID)
}
However this provides the error of “Result of ‘DataView’ initializer is unused” then an error of “Failed to open URL” with the new newUniqueID since it has tried to use it as a regular URL.
BM0 is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.