NSURL "URLByAppendingPathExtension:" bug? - ios

NSURL's URLByAppendingPathExtension: doesn't seem to be acting properly for me. I am trying to add a path to the end of an existing URL. For example:
[[NSURL URLWithString:#"https://www.example.com"] URLByAppendingPathExtension:#"/10392983/example"]
Should be returning an URL with http://www.example.com/10392983/example according to the documentation. Instead, it is escaping the slashes. I can achieve the same result converting to the URL to an absolute string and using regular NSString operations, but the above seems like it would be a lot more elegant. Any ideas as to which NSURL method to use to achieve this result or is this truly a bug?

For what you are trying to do, you should use:
URLByAppendingPathComponent
According to the docs, URLByAppendingPathExtension is for extensions like .html. It's a little vague, but from the docs:
If the original URL ends with one or more forward slashes, these are
removed from the returned URL. A period is inserted between the two
parts of the new URL.
This makes me think that this should only be used for appending things like .html and .php to URLs. Just use URLByAppendingPathComponent instead.

NSURLs can get confusing and weird.
Documentation for URLByAppendingPathExtension says that:
"If the original URL ends with one or more forward slashes, these are removed from the returned URL. A period is inserted between the two parts of the new URL."
"removed from the returned URL" implies that these '/' forward slash characters will be replaced with their escaped counterparts (that is a % sign for forward slashes).
UrlByAppendingPathComponent from #scott's answer does not appear to work in my tests? The method needs to be spelled 'URLByAppendingPathComponent' with a capitalized 'URL' and not the lowercase 'Url' .
Try this:
[NSURL URLWithString:[#"https://www.example.com" stringByAppendingPathComponent:#"/10392983/example"]]
That will produce the NSURL with a similar level of elegance-complexity. You can also tack on any number of "stringByAppendingPathComponent" methods to produce your NSURL.
Conversely here's an example of scott's code above:
NSURL*combined = [baseURL URLByAppendingPathComponent: stringPath];

Related

How to equate a value in encoded URL?

We have multiple URL's, wanted to compare
https://securepubads.g.doubleclick.net/gampad/ads?ssp%3D0
if you can see in below URL ssp=0 can be present anywhere. I want a wildcard code that can be replace ssp%3D0.
https://securepubads.g.doubleclick.net/gampad/ads?pvsid%3D326202298883096&correlator%3D3063969260360993&output%3Dldjh&gdfp_req%3D0&vrg%3D2022030600&ptt%3D08&impl%3Dfifs&iu_parts%3D3302333,profpromo,medscpnewsdesktop&enc_prev_ius%3D/0/0/2,/0/0/2,/0/0/2,/0/0/2,/0/0/2,/0/0/2,/0/0/2&prev_iu_szs%3D320x90|828x90|980x90|980x290,320x90|300x290|300x600|300x390,320x90|0x2,0x2,320x90|2x9,320x90|2x9,320x90|828x90|828x90&fluid%3Dheight,height,height,0,height,height,height&ifi%3D0&adks%3D838338968,2333289989,3296303628,0699999960,3982038306,3982003000,883988630&sfv%3D0-0-38&ecs%3D20220323&fsapi%3Dfalse&prev_scp%3Dpos%3D003&ad_slot%3Dads-pos-003&mnetPageID%3D9&mnetCC%3DIN&mnetCV%3D0&mnetUGD%3D3&mnetCID%3D8CU9I96G3&hb_abt%3Dhb&mnetDNB%3D0|pos%3D022&ad_slot%3Dads-pos-022&mnetPageID%3D3&mnetCC%3DIN&mnetCV%3D0&mnetUGD%3D3&mnetCID%3D8CU9I96G3&hb_abt%3Dhb&mnetDNB%3D0|pos%3D900&ad_slot%3Dads-pos-900|pos%3D009&ad_slot%3Dads-pos-009|pos%3D622&ad_slot%3Dads-pos-622|pos%3D822&ad_slot%3Dads-pos-822|pos%3D030&ad_slot%3Dads-pos-030&mnetPageID%3D0&mnetCC%3DIN&mnetCV%3D0&mnetUGD%3D3&mnetCID%3D8CU9I96G3&hb_abt%3Dhb&mnetDNB%3D0&eri%3D0&cust_params%3Dlif%3D0&val%3D0&vit%3D&pbs%3D&st%3D0&tar%3D0&actid%3D0&occ%3D0&sa%3D0&tc%3D0&ct%3D0&pb%3D0&usp%3D0&pf%3D0&masid%3D0&gd%3D0&pbp%3D&ssp%3D08&ac%3D0&art%3D0&as%3D0&cg%3D0&ssp%3D0&scg%3D0&ck%3D0&pub%3D0&pc%3Dhp&auth%3D0&spon%3D08&env%3D0&envp%3Dprod&ep%3D0&
Sorry for bit confused question

URL String validation in iOS using NSRegularExpression

I would like to validate whether an NSString can be converted to a valid NSURL. I know that using URLWithString will make a URL, but it is not always valid. Additionally, I don't want to make a web call every time my user enters a string to verify the URL as that is not battery/data efficient, and it relies on having an active web connection which is not always the case. I came across this site various URLs, and I am now attempting to copy the regex they used and convert it to NSRegular Expression. I've been using this helpful cheatsheet to try and convert it, but to no avail. I stored the regex as a const like so:
static NSString * const urlPattern = #"_^(?:(?:https?|ftp)://)(?:\S+(?::\S*)?#)?(?:(?!10(?:\.\d{1,3}){3})(?!127(?:\.\d{1,3}){3})(?!169\.254(?:\.\d{1,3}){2})(?!192\.168(?:\.\d{1,3}){2})(?!172\.(?:1[6-9]|2\d|3[0-1])(?:\.\d{1,3}){2})(?:[1-9]\d?|1\d\d|2[01]\d|22[0-3])(?:\.(?:1?\d{1,2}|2[0-4]\d|25[0-5])){2}(?:\.(?:[1-9]\d?|1\d\d|2[0-4]\d|25[0-4]))|(?:(?:[a-z\x{00a1}-\x{ffff}0-9]+-?)*[a-z\x{00a1}-\x{ffff}0-9]+)(?:\.(?:[a-z\x{00a1}-\x{ffff}0-9]+-?)*[a-z\x{00a1}-\x{ffff}0-9]+)*(?:\.(?:[a-z\x{00a1}-\x{ffff}]{2,})))(?::\d{2,5})?(?:/[^\s]*)?$_iuS";
I have been attempting to convert this by adding escape characters in front of all special characters listed in the cheatsheet, but it doesn't seem to be working properly. Has anyone here has any luck getting #diegoperini's URL regex to work in Objective-C or Swift using NSRegularExpressions?

What happens to the second forward slash?

Why does
"http://blah/".stringByAppendingPathComponent("foo")
return
"http:/blah/foo"
Notice the dropped forward slash.
If you read the documentation for stringByAppendingPathComponent, you will see this statement:
Note that this method only works with file paths (not, for example,
string representations of URLs).
The implementation of stringByAppendingPathComponent is "fixing" what it perceives as a badly formed file path.
You should either be using NSURLs or stringByAppendingString.
Method stringByAppendingPathComponent is supposed to be used with file paths and not with urls so my feeling is that method logic is considering // a mistake and fixing it.
You need to first convert your link to NSURL, then you can use URLByAppendingPathComponent:
if let blaFooURL = NSURL(string:"http://blah/")?.URLByAppendingPathComponent("foo") {
println(blaFooURL) // "http://blah/foo"
}

Attaching parameters to the URL of a Rails route

This is a silly question but weird enough I Googled it, I am sure i had seen it before in Rails guides but now couldn't find it.
I want to attach parameters to my URL.
My initial url is this: "http://localhost:3000/pharmacy/patients"
Now I attach one URL with string concatination in JavaScript and it will be this:
"http://localhost:3000/pharmacy/patients?provider=234"
And still good.
Now I want to attach a second parameter named thera_class and its values are strings with spaces between them like "Nasal Congestion"
If I want to also concatenate that second parameter to it, How would the URL look like?
The way it would look is:
http://localhost:3000/pharmacy/patients?provider=234&thera_class=Nasal Congestion
To be extra strict, spaces are replaced by %20 in the URL:
http://localhost:3000/pharmacy/patients?provider=234&thera_class=Nasal%20Congestion

How do SO URLs self correct themselves if they are mistyped?

If an extra character (like a period, comma or a bracket or even alphabets) gets accidentally added to URL on the stackoverflow.com domain, a 404 error page is not thrown. Instead, URLs self correct themselves & the user is led to the relevant webpage.
For instance, the extra 4 letters I added to the end of a valid SO URL to demonstrate this would be automatically removed when you access the below URL -
https://stackoverflow.com/questions/194812/list-of-freely-available-programming-booksasdf
I guess this has something to do with ASP.NET MVC Routing. How is this feature implemented?
Well, this is quite simple to explain I guess, even without knowing the code behind it:
The text is just candy for search engines and people reading the URL:
This URL will work as well, with the complete text removed!
The only part really important is the question ID that's also embedded in the "path".
This is because EVERYTHING after http://stackoverflow.com/questions/194812 is ignored. It is just there to make the link, if posted somewhere, if more speaking.
Internally the URL is mapped to a handler, e.g., by a rewrite, that transforms into something like: http://stackoverflow.com/questions.php?id=194812 (just an example, don't know the correct internal URL)
This also makes the URL search engine friendly, besides being more readable to humans.

Resources