What's the best way to get an url minus its query string in Objective-C? An example:
Input:
http://www.example.com/folder/page.htm?param1=value1¶m2=value2
Output:
http://www.example.com/folder/page.htm
Is there a NSURL
method to do this that I'm missing?
We should try to use NSURLComponents
You might fancy the method
replaceOccurrencesOfString:withString:options:range:
of theNSMutableString
class. I solved this by writing a category forNSURL
:This way, I can send this message to existing
NSURL
objects and have a newNSURL
object be returned to me.I tested it using this code:
and I got the following output (minus the time stamps):
Note that the question mark ('?') still remains. I will leave it up to the reader to remove it in a secure way.
You could try using
query
ofNSURL
to get the parameters, then strip that value usingstringByReplacingOccurrencesOfString
ofNSString
?Note, the final URL will still end with ?, but you could easily strip that as well if needed.
I think what you're looking for is
baseUrl
.Swift Version
Hope this helps!
What you probably need is a combination of url's host and path components: