Can't decode NSString well - ios

I had my server encoding in ISO-8859-1 and decided to chango into UTF-8. The problem is that the filenames creates in ISO now crashes because I change the encoding type in my code. I try to connect with an iOS app and show the directories and files, the news are shown well but the olds with ISO not.
How can I detect if the filename is in one or other encoding to process in the right way each one? Because now, the filename in ISO it can be represent in UTF-8 but the string to access is not the same. (Ex: %E1p%F1 --> %C3%A1p%C3%B1)
I try this, but it doesn't work:
NSString *isoL = [item.href stringByAddingPercentEscapesUsingEncoding:NSISOLatin1StringEncoding];
char const *iso2utf = [isoL UTF8String];
NSString *utf = [[NSString alloc]initWithCString:iso2utf encoding:NSUTF8StringEncoding];

You need to have the server declare the content encoding of the response, much like how HTTP does.
I don't believe it's possible to auto-detect ISO-8859-1/UTF-8 text encoding.

Related

iOS Unicode Encoding

I am getting this kind of data ElbowWristHand_DeQuervian\U00e2\U0080\U0099s Tenosynovitis through a web service, But actually the content is "ElbowWristHand_DeQuervian's". I have followed all the methods mentioned in the following link:
Using Objective C/Cocoa to unescape unicode characters, ie \u1234
but still i am unable to convert the unicode characters to the proper string. Please suggest.
Thanks
While initializing string us unicode string encoding -
NSString *dataString = [[NSString alloc] initWithData:recievedData usingEncoding:NSUnicodeStringEncoding]

Failed to read the gzip compressed string in Objective-C?

As the title says,i am using GZip to compress and decompress the string.But in between that i want to read the compressed string,how do i read that unknown compressed string?.
What i tried so far:
Using GZip(from github),i tried compressing the string as below...
//UML string
NSString *plantUmlString = #"#startuml\n Bob -> Alice : hello \n#enduml"
String compression...
NSData *originalData = [plantUmlString dataUsingEncoding:NSASCIIStringEncoding];
NSString *compressedString = [[originalData gzippedData] base64EncodedStringWithOptions:kNilOptions];
NSLog(#"%#", compressedString);//H4sIAAAAAAAAA8tIzcnJV0gvrSwuKcrMSw8tBhKuecn5KQCRj54cGQAAAA
Where i am struggling:
compressedString is returning "H4sIAAAAAAAAA8tIzcnJV0gvrSwuKcrMSw8tBhKuecn5KQCRj54cGQAAAA"
instead of "SzIrIxBAICt9oGS0",this is the actual string i should get.If i decode the generated compressedString,i am getting nill...And if i use UTF-8 encoding i am getting "null"..How do i read the actuall string here?
NOTE: if i decompress the above compressed data,i am getting original string before compressing...
NSData *decodedData = [[NSData alloc] initWithBase64EncodedString:compressedString options:0];
NSString *decompressedString=[[NSString alloc] initWithData:[decodedData gunzippedData] encoding:NSASCIIStringEncoding];
NSLog(#"%#",decompressedString);
//Correct: decompressedString returning me the original string before compressing...
What i am trying to do:
Trying to generate a flowchart using the PlantUML Server,which accepts the compressed UML string and returns me the flowchart image...
Some more information : If i compress the above UML string in android(which is available in github) i am getting "SzIrIxBAICt9oGS0" as the string...But in Objective-C i am reading entirely different string...
Am i doing something worng with the encoding?...Any advice/solution is really helpful...
According to the sample code at http://plantuml.sourceforge.net/codephp.html , it uses compression level 9, so you probably want to use that, if you want to exactly match that string.

ios issue with stringByAddingPercentEscapesUsingEncoding

In my app I need to send some parameters to the url, when I am trying with the stringByAddingPercentEscapesUsingEncoding it is not converting correctly. If I am not using this encoding I am getting null(Exception) from the nsurl.Here is me code.
http://www.mycompurl.co?message=xyz&id=____ here I am sending the id 1 or 2 or any number.
when I convert this string to url by using stringByAddingPercentEscapesUsingEncoding I got
"http://www.mycompurl.co?message=xyz&id=**%E2%80%8B**1" (when I send 1 as parameter). Then I got the 0 data from the Url.
str = [NSString stringWithFormat:#"%#?message=xyz&id=​%#",Application_URL,bootupdateNew];
str = [str stringByAddingPercentEscapesUsingEncoding: NSUTF8StringEncoding];
url=[NSURL URLWithString:str];
NSError* error = nil;
data1 = [NSData dataWithContentsOfURL:url options:NSDataReadingUncached error:&error];
Thank you In advance
Basics
A URL is composed of several components.
Each component has its own rule how the component's source string must be encoded, so that this component becomes valid within the URL string.
Applying stringByAddingPercentEscapesUsingEncoding: will never always produce a correct URL if the string consists of more than one component (and if we assume, we have an unbounded set of source strings - so that the encoded string actually differs from the source string).
It even won't work always with a string which represents any single component.
In other words, for what's worth, stringByAddingPercentEscapesUsingEncoding: should not be used to try to make a URL out of several components. Even getting the URL query component correctly encoded is at least error prone, and when utilizing stringByAddingPercentEscapesUsingEncoding: it still remains wonky. (You may find correct implementations on SO, though - and I posted one myself).
But now, just forget about it:
It took awhile for Apple to recognize this failure, and invented NSURLComponents. It's available since iOS 7. Take a look! ;)

Determining Issue With Retrieving JSON from URL in iPhone

Let me start off by saying that I am not particularly trying to find a solution, just the root cause of the problem. I am trying to retrieve a JSON from a url. In browser, the url call works just fine and I am able to see the entire JSON without issue. However, in x-code when simply using NSURLConnection, I am getting data bytes, but my NSString is null.
theString = [[NSString alloc] initWithData:urlData encoding:NSUTF8StringEncoding];
After doing some research I have found that I am probably trying to use the wrong encoding. I am not sure what type of encoding is being used by the url, so on first instinct I just tried some random encoding types.
NSString* myString = [[NSString alloc] initWithData:data encoding:NSASCIIStringEncoding];
NSString* myString2 = [[NSString alloc] initWithData:data encoding:NSUTF16StringEncoding];
NSString* myString3 = [[NSString alloc] initWithData:data encoding:NSWindowsCP1252StringEncoding];
NSASCIIStringEncoding and NSWindowsCP1252StringEncoding is able to bring back a partially correct JSON. It is not the entire JSON thatI am able to view in the browser, and some characters are a little messed up, but it is something. To try and better determine what encoding was used, I decided to use the following method to try and determine it by looking at what encoding returned.
NSError *error = nil;
NSStringEncoding encoding;
NSString *my_string = [[NSString alloc] initWithContentsOfURL:url
usedEncoding:&encoding
error:&error];
My NSStringEncoding value is 3221214344. And this number is consistent everytime I run the app. I can not find any NSStringEncoding values that even come close to matching this.
My final question is: Is the encoding used for this url not consumable by iOS, is it possible that multiple types of encoding was used for this url, or is there something else that I could be doing wrong on my end?
It's best not to rely on Cocoa to figure out the string encoding if possible, especially if the data might be corrupted. A better approach would be to check if the value indicated by the HTTP Content-Type header specifies a character set like in this example:
Content-Type: text/html; charset=ISO-8859-4
Once you're able to parse and retrieve a character set name from the Content-Type header, you need to convert it to an NSStringEncoding, first by passing it to CFStringConvertIANACharSetNameToEncoding, and then passing the returned CF string encoding to CFStringConvertEncodingToNSStringEncoding. After that, you can initialize your string using -[NSString initWithData:encoding:].
NSData *HTTPResponseBody = …; // Get the HTTP response body
NSString *charSetName = …; // Get a charset name from the Content-Type HTTP header
// Get the Core Foundation string encoding
CFStringEncoding cfencoding = CFStringConvertIANACharSetNameToEncoding((CFStringRef)charSetName);
// Confirm this is a known encoding
if (cfencoding != kCFStringEncodingInvalidId) {
// Initialize the string
NSStringEncoding nsencoding = CFStringConvertEncodingToNSStringEncoding(cfencoding);
NSString *JSON = [[NSString alloc] initWithData: HTTPResponseBody
encoding: nsencoding];
}
You still may run into problems if the string data you're working with is corrupted. For example, in the above code snippet, perhaps charSetName is UTF-8, but HTTPResponseBody can't be parsed as UTF-8 because there's an invalid byte sequence. In this situation, Cocoa will return nil when you try to instantiate your string, and short of sanitizing the data so that it conforms to the reported string encoding (perhaps by stripping out invalid byte sequences), you may want to report an error back to the end user.
As a last-ditch effort — rather than reporting an error — you could initialize a string using an encoding that can handle anything you throw at it, such as NSMacOSRomanStringEncoding. The one caveat here is that unicode / corrupted data may show up intermittently as symbols or unexpected alphanumerics.
Even though it seems that the answer has been provided in the comments (using iso-8859-1 as the correct encoding) I thought it worthwhile to discuss how I would go about debugging this problem.
You said that the Desktop Browser (Chrome) can digest the data correctly, so let's use that:
Enable Developer Tools https://developers.google.com/chrome-developer-tools/
When the Dev Tools window is open, switch to "network" and execute your call in that browser tab
check the output by clicking on the request url - it should give you some clue.
If that doesn't work, tools like Postman can help you to recreate the call before you implement it on the device

iOS special characters, strange issue

I've got a very strange issue. I started an iOS App about three years ago (iOS-SDK 3.0), and since then went through the SDKs 4.0 and 5.0. Since 5.0 (or maybe 5.1) I suddenly started having problems with German special chars (ä ö ü ß).
Now I can't even initialize an NSString with special chars, this line:
NSString *str = #"abcäxyz";
gives the following warning:
Input conversion stopped due to an input byte that does not belong to the input codeset UTF-8
And this one:
NSLog(#"%#", strTemp);
gives:
abc
So it's stopping at the first special char. In other projects everything is fine. I can work with special chars without any problems.
Is it a configuration problem?
EDIT: Obviously it is a problem with the file encoding.
file -I myFile
is giving:
text/x-c++; charset=unknown-8bit
Trying to convert it with iconv gives me:
conversion from unknown-8bit unsupported
What happens when you use the UTF-8 codes to initialize the string? Like so:
NSString *s = [NSString stringWithFormat:#"%C", 0xc39f]; // should be ß
As far as I know you should also be able to do this, but haven't tested it:
NSString *s = [NSString stringWithUTF8String:"0xc39f"];
Try those and see what happens. There's a number of sites around that keep UTF-8 code tables for special characters, e.g. this one.
As long as your file is encoded UTF-8, #"abcäxyz" should be fine, but the explicit form of embedding a literal unicode characters is \u????.
- (void)testGermanChar
{
NSString *expected = #"abc\u00E4xyz";
NSString *actual = #"abcäxyz";
STAssertEqualObjects(expected, actual, #"the two strings should be equivalent");
}
SOLVED: Changed the file encoding in Xcode:
Click on the file you want to change the encoding of, then open the right panel (whats the name of this pane actually? any idea?) to edit the properties. There you see "Text Encoding" under "Text Settings". That is all.

Resources