It's quite easy to open a URL
in SwiftUI, e.g. as I've mentioned in this answer, however, I'm interested in opening the following strings in the web browser (e.g. Safari) with the default search engine:
samplestring
8883notaurl
Is there an option to open the query in a default search engine, instead of just composing a URL like this:
And then opening it as a regular URL?
CodePudding user response:
You can use x-web-search://
scheme with x-web-search://?[SearchValue]
.
Sample code:
let urlStr = "x-web-search://?[SearchValue]"
if let url = URL(string: urlStr) {
if UIApplication.shared.canOpenURL(url) {
UIApplication.shared.open(url, options: [:]) { didEnd in
print("Did End: \(didEnd)")
}
} else {
print("Can't open URL")
}
} else {
print("Isn't URL")
}