NSString convert string to NSData using encoding - ios

I have got small issue in my app.
I make NSMutableURLRequest and send body to my webserice.
jsonRequest = #"{\"s\":\"deón\"}";
NSData *requestData = [NSData dataWithBytes:[jsonRequest UTF8String] length:[jsonRequest length]];
I need change "ó" symbol to \u00F3.
Any idea?

I would suggest you another way around if you want to deploy on target >=iOS5. It involves more steps, but you can be sure about the correct result, it's less error prone.
NSDictionary * jsonDict = #{ #"s" : #"deón" };
NSData * requestData = [NSJSONSerialization dataWithJSONObject:jsonDict options:0 error:nil];

convert in UTF-8
NSData *data = [arrayString dataUsingEncoding:NSUTF8StringEncoding];

Try this code it will near to your requirement
NSString *jsonRequest = #"{\"s\":\"deón\"}";
const char *c = [jsonRequest cStringUsingEncoding:NSISOLatin1StringEncoding];

Related

Using base64 string with NSURLConnection

I am using a NSURLConnect call and I get unsupported URL when I have a space in my query string. To fix this I converted the string to base64 and I am still getting errors. How do I send a base64 string though NSUrlConnect successfully? If this isn't possible, how do I safely send a string with a space in it?
NSData *convertData = [queryWS dataUsingEncoding: NSUnicodeStringEncoding];
queryWS = [convertData base64EncodedStringWithOptions:kNilOptions];
NSURLRequest *urlRequest =
[NSURLRequest requestWithURL:[NSURL URLWithString:queryWS]];
NSURLResponse *response = nil;
NSError *error = nil;
NSData *data = [NSURLConnection sendSynchronousRequest:urlRequest
returningResponse:&response
error:&error];
if (!error) {
responseDictionary =
[NSJSONSerialization JSONObjectWithData:data
options:0
error:&parseError];
You do not want to convert the URL string to base64 encoding. What you want is to properly escape any special characters in the URL. Please see the NSString stringByAddingPercentEscapesUsingEncoding: method or NSString stringByAddingPercentEncodingWithAllowedCharacters: if you only need to support iOS 9 or later.
NSString *queryString = #"SOME_QUERY_STRING";
NSString *encodedQueryString = [queryString stringByAddingPercentEncodingWithAllowedCharacters:[NSCharacterSet URLQueryAllowedCharacterSet]];

How to decrypt nsstring using RNCryptor after conversion from NSData

Is there anyway to decrypt a NSString using RNcryptor that has been converted from NSData? I keep getting the unknown header error. Here is what I am doing to encrypt:
NSData *data = [#"FeedMeMorePizzaPlease" dataUsingEncoding:NSUTF8StringEncoding];
NSData *encryptedData = [RNEncryptor encryptData:data
withSettings:kRNCryptorAES256Settings
password:#"pizzaHutIsTheWorst"
error:&error];
NSString *encString = [encryptedData base64EncodedStringWithOptions:0];
I then take encString, put it in a text file on my server and then:
NSURL *gUrl = [NSURL URLWithString:#"https://myurlissupersecret.com/cheese.txt"];
NSString *sillyString = [NSString stringWithContentsOfURL:gUrl encoding:(NSUTF8StringEncoding) error:nil];
NSData *blindData = [sillyString dataUsingEncoding:0];
NSData *decryptedData = [RNDecryptor decryptData:blindData withSettings:kRNCryptorAES256Settings password:#"pizzaHutIsTheWorst" error:&error];
Is there anyway to do this? Or do I have to always encrypt / decrypt a file?
You missed a step in the decryption. sillyString is the base64 encoded string. You need to convert the base64 encoded string into NSData. Here is the updated code you need to decrypt:
NSURL *gUrl = [NSURL URLWithString:#"https://myurlissupersecret.com/cheese.txt"];
NSString *encString = [NSString stringWithContentsOfURL:gUrl encoding:NSUTF8StringEncoding error:nil];
NSData *encryptedData = [[NSData alloc] initWithBase64EncodedString:encString options:0];
NSData *decryptedData = [RNDecryptor decryptData:encryptedData withSettings:kRNCryptorAES256Settings password:#"pizzaHutIsTheWorst" error:&error];

iOS : How do i convert NSString to NSMutableData?

I am trying to convert my NSString to NSMutableData without the use of encoding. How can I accomplish this?
How about this?
NSString *str = #"grazz";
NSMutableData *data1 = [[str dataUsingEncoding:NSUTF8StringEncoding] mutableCopy];
NSMutableData *data2 = [NSMutableData dataWithBytes:[str UTF8String] length:[str length]];
ps: I'm not sure about "without encoding", what did you mean by that?
As pointed out here: NSString value to NSData, you can use dataUsingEncoding to convert an NSString to NSData.
To place it in a NSMutableData container, simply make a mutableCopy, like so:
NSString *string = #"MyString";
NSMutableData *mutableData = [[string dataUsingEncoding:NSUTF8StringEncoding] mutableCopy];
As for doing this "without the use of encoding", the string will always be encoded. Even if you don't set any specific encoding the string will still be encoded. I think the solution above should solve your problem.
why the question is downvoted?
NSString* str = #"teststring";
NSData* data = [str dataUsingEncoding:NSUTF8StringEncoding];
NSMutableData *yourdata = [data mutableCopy];
first you have to convert to NSData and then to NSMutableData. From my knowledge, we cannot convert directly to NSMutableData from NSString.

Json crash when sending space and dot

I am trying to request a URL and I am getting error saying
"Data parameter is nil".
I had found that the variables used has got space dot(.). I think this is the problem that is being caused by the URL. So is there any way to send URL having space and dot without crashing?
NSURL *url = [[NSURL alloc]initWithString:[NSString stringWithFormat:#"192.168.1.5/mobileapp?/signin=%#&%#",username,password]];
NSError *errors;
NSData *data = [NSData dataWithContentsOfURL:url];
NSDictionary *json = (NSDictionary *)[NSJSONSerialization JSONObjectWithData:data options:kNilOptions error:&errors];
Try using NSUTF8StringEncoding
NSString *myUnencodedString = [NSString stringWithFormat:#"192.168.1.5/mobileapp?/signin=%#&%#",username,password]
NSString *encodedString = [myUnencodedString stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding];
NSURL *myURL = [[NSURL alloc] initWithString:encodedString]

How to request url which has space and dot in ios?

I am trying to request the following URL and i am getting an error saying "Data parameter is nil". i had found that the 'seller_name' has got space and 'amount' has got dot(.). I think this is the problem that is being caused by the URL. So is there any way to send URL having space and dot without loosing the information?
NSURL *url1 = [[NSURL alloc]initWithString:[NSString stringWithFormat:#"192.168.1.85/localex_postsale.html?contactid=%#&exchangeid=%#&token=%#&buyerid=%#&seller_name=%#&desc=%#&amount=%#&sellerid=%#",contactid,exchangeid,token,buyervalue,sellers,_Description,_Amount,contactid]];
NSError *errors1;
NSData *data1 = [NSData dataWithContentsOfURL:url1];
NSDictionary *json1 = (NSDictionary *)[NSJSONSerialization JSONObjectWithData:data1 options:kNilOptions error:&errors1];
Take a look at the -stringByAddingPercentEscapesUsingEncoding: method in NSString, e.g.:
NSString *myUnencodedString = [NSString stringWithFormat:#"192.168.1.85/localex_postsale.html?contactid=%#&exchangeid=%#&token=%#&buyerid=%#&seller_name=%#&desc=%#&amount=%#&sellerid=%#",contactid,exchangeid,token,buyervalue,sellers,_Description,_Amount,contactid]
NSString *encodedString = [myUnencodedString stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding];
NSURL *myURL = [[NSURL alloc] initWithString:encodedString]
...
Cf.: Apple documentation.
It is happening because URL should not contain white space. It should be encode to %20 if there is any space. We can encode space and special character in NSString using NSUTF8StringEncoding as below
NSString *string = [[NSString stringWithFormat:#"192.168.1.85/localex_postsale.html?contactid=%#&exchangeid=%#&token=%#&buyerid=%#&seller_name=%#&desc=%#&amount=%#&sellerid=%#",contactid,exchangeid,token,buyervalue,sellers,_Description,_Amount,contactid]stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding];
NSURL *url1 = [[NSURL alloc]initWithString:string];
NSError *errors1;
NSData *data1 = [NSData dataWithContentsOfURL:url1];
NSDictionary *json1 = (NSDictionary *)[NSJSONSerialization JSONObjectWithData:data1 options:kNilOptions error:&errors1];

Resources