Storing URL string with special characters in NSString in Objective-C - ios

I am trying to use Freebase in an iOS app, and queries are constructed using URL's. The URL's contain multiple special characters ({}, :) but I would like to be able to use the URL strings so I can download the data from Freebase.
https://www.googleapis.com/freebase/v1/mqlread?query=[{"type":"/music/album","name":null,"artist":{"id":"/en/bob_dylan"},"limit":3}]&cursor
When loading the URL in Safari, the browser actually converts that string to this: https://www.googleapis.com/freebase/v1/mqlread?query=%5B%7B%22type%22:%22/music/album%22,%22name%22:null,%22artist%22:%7B%22id%22:%22/en/bob_dylan%22%7D,%22limit%22:3%7D%5D&cursor
Any help would be greatly appreciated.

You can use this method on an NSString to add the percent escapes for you.
- (NSString *)stringByAddingPercentEscapesUsingEncoding:(NSStringEncoding)encoding

Related

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?

How To Avoid The Firewll error due to an Apostrophe (’) in a URL generated

I'm working on QlikView and there is requirement that a URL would be generated using the filters that I have selected in QV and then it would be parse to another application. The url contains the values that I have selected in QV. We are facing an issue due to the and apostrophe (') in the user name.
Below is url generated. You could see, the generated url is not completely treated as a URL due to the apostrophe.
http://abcworld.com/berlin/cgi-bin/berlinisapi.dll?b_action=berlinViewer&ui.action=run.prompt=false&p_Type=E&p_Tra=Paul O'Donnell&p_AdjType=O&p_UD=2014-08-11
How to overcome this issue? Is there any special character that I could replace it with?
Thanks in advance.
You would want to encode the apostrophe as %27. This is called URL Encoding and is useful when you need to insert characters in a URI that can't normally be represented in a URI, or otherwise have special meaning, like a question mark. Spaces are often encoded as %20. So your final URL might be:
http://abcworld.com/berlin/cgi-bin/berlinisapi.dll?b_action=berlinViewer&ui.action=run.prompt=false&p_Type=E&p_Tra=Paul%20O%27Donnell&p_AdjType=O&p_UD=2014-08-11

AFNetworking and URL construction - can I change the behaviour?

I'm busy with web service and an iOS app. So far I haven't had that many issues. However, there is a web service that needs the final URL request to be in a certain format. Something along the lines of:
...?IncludedUserIds[]=1357213,286476&..
These parameters are constructed from an NSDictionary and NSString. Now when I add the comma - the end URL that makes the request ends up like this:
..?IncludedUserIds=%5B%5D1357213%2C286476&...
It seems that AFNetworking 2.0 has converted the square brackets into =%5B%5D and the comma into: %2C
Obviously, the web service has no idea what this means and fails.
Is there a way to keep the final Url as I need it to be? Why do these conversions happen and where can I learn more about this sort of thing?
AFNetworking keep the final url encoded, that is the best way, since you could have special characters in you query string that could break all. Instead, you should decode the url server side, for example in PHP you have methods, like urldecode or rawurldecode (eg: 'foo%20bar%40baz' ->'foo bar#baz'), or in ASP it should be kept automatically.
Hope it helps

Datausingencoding that doesn't replace plus signs

I'm looking for a datausingencoding parameter that doesn't swallow up plus signs. I was using NSASCIIENCODING but since I'm trying to send a uiimage to the server and the base64 string had plus signs in them, it seems like that form of encoding takes out the plus sign sending a modified encoded string to the server thereby not allowing the image to be decoded server side. I'm looking for something that won't alter the base64 string.
Nevermind guys, here is the solution I found on stackoverflow
thanks, now I figured it out. It seems I needed to run my string through the stringByAddingPercentEscapesUsingEncoding: first, then I needed to run it through replaceOccurrencesOfString:#"+" withString:#"%2B" and several more of those replaces for different characters, because stringByAddingPercentEscapesUsingEncoding: doesn't escape them all

Escape bad characters for iOS UILabel?

I'm feeding some NSString data (forum posts queried from my website) into a UILabel as part of my first app. Thing is, sometimes, depending on the content of the post, the Label goes entirely blank. I've tinkered enough to discern that there are certain characters that cause the problem, but I can't quite pin down the full set.
Is there a collected list of character types to watch out for with this kind of thing? And even better, is there a method for escaping them, or automatically converting them into something more acceptable?
Thank you for helping out a n00b!
Looks like you have whitespace or new line in your string:
Try this:
NSString* labelText = [stringFromWebsite stringByTrimmingCharactersInSet:[NSCharacterSet whitespaceAndNewlineCharacterSet]]
I am trying to give you some clue even you haven't put piece of code here in your question.
You should firstly try to encode that coming String by using appropriate method like encoding:NSUTF8StringEncoding and still and try to set decoded string to your UIlabel.
Another alternative is that you can create some regex for filtering purpose of that coming string. you can find many of similar thread over the google.

Resources