How to send Numbers and Characters in one textField to Server? - ios

I have one text field, in that text field I want to add numbers, characters like this (£ 12 /hr) and need to post to server but its taking either number or charecter I want to send both. if any suggestion it would be Appreciate.

Get the textfield's text and encode it before sending to server.
NSString *post = textfield.text
NSData *postData = [post dataUsingEncoding: NSUTF8StringEncoding allowLossyConversion:YES];
NSUTF8StringEncoding encoding will encode into proper data. After that send it to server using NSMutableURLRequest

Related

HTML-encode special characters for JSON serialization

I have a web service that sends and accepts JSON, and handles all special characters by HTML-encoding them. I can parse and read the web service data perfectly fine using the following (error checking removed for brevity):
cleanJsonString = [self stringByDecodingXMLEntities:jsonString];
NSDictionary *JSON = [NSJSONSerialization JSONObjectWithData: [cleanJsonString dataUsingEncoding:NSUTF8StringEncoding allowLossyConversion:YES]
options: NSJSONReadingMutableContainers
error: &parseError];
The problem comes into play when I need to serialize user input to send data back to the server. I'm currently using the following:
NSData *jsonData = [NSJSONSerialization dataWithJSONObject:myDictionaryObject
options:NSJSONWritingPrettyPrinted
error:&parseError];
NSString *myString = [[NSString alloc] initWithData:jsonData encoding:NSUTF8StringEncoding];
The problem is that this does not account whatsoever for special characters. No errors occur on the client side, but the web service is receiving bad data. I know I could brute force it by recursing the dictionary object and encoding every string before serializing. But would it be safe to just do one single encode operation on the entire JSON string without corrupting it? Or, even more ideally, is there such a parameter that would handle encoding during the serialization process. Any advice or suggestions greatly appreciated.

Wierd form of encoding in NSURL(inviolves backslashes followed by frontslashes after the "http:)

I have to pass JSON dictionary as POST data to a Webservice. One key involves an Amazon S3 URL string.
The sample request json which works has the URL as....
https:\/\/myappbucket.s3.amazonaws.com\/2014230407_102323.jpg?response-content-type=image\/png&Signature=123456%3D&Expires=139756222548&AWSAccessKeyId=ABCDEF
Notice the backslashes just before the forwardslashes? I have never seen a URL like that, but thats how I'm supposed to pass it.
I tried
stringByAddingPercentEscapesUsingEncoding
and
stringByReplacingPercentEscapesUsingEncoding
while using NSASCIIStringEncoding and NSUTF8StringEncoding
Can anyone make sense of this?
if we try to convert url into legal url trough stringByAddingPercentEscapesUsingEncoding than it adds all percent escapes necessary to convert the receiver into a legal URL string.Uses the given encoding to determine the correct percent escapes.
if we use stringByReplacingPercentEscapesUsingEncoding than it replaces all percent escapes with the matching characters as determined by the given encoding.
mostly to get valid url, we can use NSUTF8StringEncoding to remove backslashes just before the forwardslashes in url.
Generally, you should use a JSON serializer library (e.g. NSJSONSerialization) in order to obtain a JSON from a JSON representation and not try to create the JSON yourself.
A JSON representation is a NSDictionary or NSArray object containing other objects which recursively represent your JSON. Your URL will be represented as a NSString.
What you need to do is simply have a valid URL as a NSString, properly encoded according RFC 3968 and assign it the JSON representation, e.g.:
NSURL* url = ...;
NSDictionary* jsonObject = #{#"url": [url path]};
Now, you can serialize the JSON representation to a JSON:
NSError* error;
NSData* jsonData = [NSJSONSerialization dataWithJSONObject:jsonObejct
options:0
error:&error];
That's it, and you don't bother how the JSON encoded string looks like (encapsulated in the NSData object as a UTF-8 character sequence).
Purposefully, when you POST this JSON, you SHOULD specify a corresponding request header:
ContentType: application/json
which lets you just use the JSON data as body data as is:
[request setValue:#"application/json" forHTTPHeaderField:#"Content-Type"];
request.HTTPBody = jsonData;
Side note: [url path] returns a URL as a string according RFC 1808 which is obsoleted by RFC 3968 since January 2005 already. Today, there are newer APIs since iOS 7.0, see NSURLComponents.

IOS passing backslash as part of url parameter to Json Web Service

I'm calling a Json Web Service wich expects two parameters. username and password.
The username I'm trying to pass includes a backslash (\) as it is a domain account eg. companyname\jamesd.
Here is my code.
NSString *sName = #"companyname\\jamesd";
NSString *URL = [NSString stringWithFormat:#"http://checkin.companyname.com:2002/Checkin.svc/checklogin?username=%#&password=pass",sName];
Problem is the backslash is somehow removed when calling the web service -
I use NSLog(...) to output what the request looks like -
<NSURLRequest http://checkin.companyname.com:2002/Checkin.svc/checklogin?username=companynamejamesd&password=pass>
I'm trying to find out how I can force include the backslash in the http request for the username.
Like Mike Weller stated in the comment you have to url-encode the arguments. You should use the stringByAddingPercentEscapesUsingEncoding: method of NSString after you create the url string something like:
NSString *URL = [NSString stringWithFormat:#"http://checkin.companyname.com:2002/Checkin.svc/checklogin?username=%#&password=pass",sName];
URL = [URL stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding];
You can change the encoding to as you want, check the iOS encoding on Apple Docs
Also make sure that you are adding the backslash when you're creating the url string.

How to decode Norwegian or any special character in a xml to display it in TextView

I am dealing with strings in my project where in i send some data (string) to a service and the service responses me XML with some data (String).Earlier i used to face probs while sending special charcters like &,",',<,>,$,%,(,) etc in the string.
Then i encoded the string before sending as below:
NSString *str=#"£&#)(;:/-.,?!'"[]{}#%^*+=Â¥$â¬><~||_~<.,?!'m"";
NSString *stringToSend=[str stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding];
then data sent is as below:
"%C2%A3%26%40%29%28%3B%3A%2F-.%2C%3F%21%27%22%5B%5D%7B%7D%23%25%5E%2A%2B%3D%C2%A5%24%E2%82%AC%3E%3C~%7C%7C_~%3C.%2C%3F%21%27m%22"
then the server handles the data as required.
While receving the data from server i do decoding as below:
NSString *strReceived=#"%C2%A3%26%40%29%28%3B%3A%2F-.%2C%3F%21%27%22%5B%5D%7B%7D%23%25%5E%2A%2B%3D%C2%A5%24%E2%82%AC%3E%3C~%7C%7C_~%3C.%2C%3F%21%27m%22";
NSString *str=[str stringByReplacingPercentEscapesUsingEncoding:NSUTF8StringEncoding];
then data received is :£&#)(;:/-.,?!'"[]{}#%^*+=Â¥$â¬><~||_~<.,?!'m" as expected.
PROBLEM/ISSUE
Now if my string contains norwegian characters like å,ø,Ø,æ,etc.
the string in response contains (%E5 for å) (%E6 for æ)and (%D8 for Ø)and (%F8 for ø).
I get the string as invalid after ececution of "stringByReplacingPercentEscapesUsingEncoding:NSUTF8StringEncoding".
Can some one suggest how to handle all the charcters received in the xml and parse them perfectly.Plz help with ur inputs...
Tnx in advance.

iOS - NSURLConnection - Proper encoding of characters in request

In my iOS application, i'm posting the request to server using NSURLConnection. The following is the code that I'm using:
NSString* str = #"http://www.myserver.com/post.php?action=rating&grade=A+&name=vasu";
NSURL *url = [NSURL URLWithString:[str stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding]];
If you see the grade, its equal to "A+"
But its posting only "A" as grade to server. Even though when I'm encoding the string appropriately, why is it not able to post the request properly?
Is it something that has to be handled on server?
The + sign is a reserved character for URL encoding (for 'space'). Although the escaped value for + should be %2B after stringByAddingPercentEscapesUsingEncoding, well... it isn't. What you can do though is to add a simple category into NSString to bypass this problem. You can find a relevant blog post here and some information on URL encoding here

Resources