Reputation: 326
I am implementing Firebase dynamic links in my iOS project. It is working fine and it is opening my iOS Home screen properly. But now I would like to extract values from url and open appropriate screen based on url.
for example: https://www.dd.com/forums/hot-deals-online/topics/smart-tv-carniva
in this url I would like to get 'hot-deals-online' and 'smart-tv-carniva' permalink which I will pass to view controller to open that screen on the app
can someone suggest me best approach for this.
Upvotes: 0
Views: 728
Reputation: 6058
Shortest is to access path
of URLComponents
object:
let url = "https://www.dd.com/forums/hot-deals-online/topics/smart-tv-carniva"
if let comps = URLComponents(string: url) {
var elements = comps.path.split(separator: "/").map(String.init)
// ["forums", "hot-deals-online", "topics", "smart-tv-carniva"]
// To build an url back, example:
var url = URL(string: comps.host!)!
url.appendPathComponent(elements[0])
url.appendPathComponent(elements[1])
// Result: www.dd.com/forums/hot-deals-online
}
P.S. calling .map(String.init)
makes in-place conversion from array of substrings to normal String
array.
Upvotes: 1