Suppose, I have following URL: https://something.com/room/order/12345555/product/543333?is_correct=true. It is kind of deeplink and I should parse its parameters and show some ViewController. I am interested in values as 12345555, 543333 and true. Actually, it is easy to get those parameters.
In order to get 12345555 or 543333, we can use pathComponents of URL which returns ["/", "room", "order", "12345555", "product", "543333"]. To get query items (is_correct: true), we can use URLComponents. Everything is clear and simple.
But suppose my link contains # as path https://something.com/room/#/order/12345555/product/543333?is_correct=true. Now, for this link, pathComponents returns just ["/", "room"] ignoring everything else. Of course, there are also problems with query parameters.
Why does # symbol affect so? How can I solve problem? Should I just replace # with something or URL from Swift contains some helper methods? Thanks.
#symbol represents the URL fragmenturl.fragment, it returns everything after #?url.fragmentreturns String and basically you cannot use convenient things likepathComponentsandURLComponentsroom/#/orderandroom/ordercan be used more-or-less interchangeably? If so something like,.replacingOccurrences(of: "/#/", with: "/")could be a reasonable first-step in transformation. Otherwise, you could do something likelet fragmentUrl = URL(string: url.fragment!, relativeTo: url)!to get just the stuff after#