Song title by stream wrong Encoding - ios

I'm working on a simple radio stream app for ios. I'm retriving song title with a KVO Observation of:
if ([keyPath isEqualToString:#"timedMetadata"])
{
for (AVMetadataItem* metadata in playerItem.timedMetadata)
{
if([metadata.commonKey isEqualToString:#"title"]){
}
}
}
With this code i can get the title. Anyway whe there are some characthers i have this result:
deep grounder feat virÃ;g
instead of
deep grounder feat viràg
So i'm trying any solution to print the correct result in the screen. I have also tried:
NSString *StringaPassata=[NSString stringWithFormat:#"%#",#"deep grounder feat virÃ;g"];
NSData *data = [stringFromUTFString dataUsingEncoding:NSUTF8StringEncoding allowLossyConversion:YES]; NSString *newStr = [[NSString alloc] initWithData:data encoding:NSUTF8StringEncoding];
The string remain the same... I have not control on the server that output the stream. To test the conversion i store in a NSString the string that cause the problem when it is downloaded from the server.
How i can convert to the string to the correct "à" ?

Try This Snippet I Have tried it
NSString *strYourKey = #"deep grounder feat viràg";
NSData *data = [strYourKey dataUsingEncoding:NSASCIIStringEncoding allowLossyConversion:YES];
NSString *dataToString = [[NSString alloc] initWithData:data encoding:NSASCIIStringEncoding];
NSLog(#"Your Converted Text is : %#", dataToString);
Output
[12134:168898] Your Converted Text is : deep grounder feat virag

Related

How to show special character in UILabel iOS

I am trying to implement an app where I would like to show some text in Spanish format. For example I would like to show "España" but in my label it shows "Espa√ɬ±a" and also it changes the text for some of other text.
How to get rid of these. If anybody could help. Thanks.
Edit: When i am getting my response it logs that Below result
Message = (
"Espa\U221a\U00c9\U00ac\U00b1a:1.3\U221a\U00c7\U00ac\U00a2/min"
);
But when i extract the value according to key from Dictionary it shows
España:1.3¢/min
It means when i am getting the value from dictionary it cant do proper decoding.
how to resolve this. Any idea..?
First convert your response String to NSData using NSUTF8StringEncoding encoding, then again convert the same data to finalString like below.
NSString *string = #"España"; //Your response String goes here
NSData *data = [string dataUsingEncoding:NSUTF8StringEncoding];
NSString *finalString = [[NSString alloc] initWithData:data encoding:NSUTF8StringEncoding];
lblTemp.text = finalString;
UPDATE 1
I think there is some error from your response, Please see below
NSString *string = #"Nu\\u0161a Florjan\\u010di\\u010d";
NSString *finalString = [NSString
stringWithCString:[string cStringUsingEncoding:NSUTF8StringEncoding]
encoding:NSNonLossyASCIIStringEncoding];
NSLog(#"finalString = %#", finalString);
Output of above code is,
finalString = Nuša Florjančič
UPDATE 2
If you want output string like "España", your desired response should be "Espa\u00F1a", Find below,
NSString *string = #"Espa\\u00F1a";
NSString *finalString = [NSString
stringWithCString:[string cStringUsingEncoding:NSUTF8StringEncoding]
encoding:NSNonLossyASCIIStringEncoding];
NSLog(#"%#",finalString);
Output is España

want to have this string "مرحبا 😀 Hello" save it to database and display back to uilabel

I am looking for solution where i want to store English + Arabic + Emoji Character to store to Database and retrieve it back while display.
Below is the code what i have used to support Emoji, after that Arabic text is not showing.
+(NSString *)emojiToSave:(NSString *)str
{
NSData *dataForEmoji = [str dataUsingEncoding:NSNonLossyASCIIStringEncoding];
NSString *encodevalue = [[NSString alloc]initWithData:dataForEmoji encoding:NSUTF8StringEncoding];
return encodevalue;
}
+(NSString *)emojiToDisplay:(NSString *)str
{
NSData *msgData = [str dataUsingEncoding:NSUTF8StringEncoding];
NSString *goodMsg = [[NSString alloc] initWithData:msgData encoding:NSNonLossyASCIIStringEncoding];
return goodMsg;
}
Can anyone pls suggest to give support for Arabic what change i should do?
Thanks in advance.
Try convert it into base64 code, then insert base64 code to database:
//Original string to base64 string
NSString *emojiString = #"مرحبا 😀 Hello";
NSData *emojiData = [emojiString dataUsingEncoding:NSUTF8StringEncoding];
NSString *base64String = [emojiData base64EncodedStringWithOptions:NSDataBase64Encoding64CharacterLineLength];
//Base64 string to original string
NSData *base64Data = [[NSData alloc] initWithBase64EncodedString:base64String options:NSDataBase64DecodingIgnoreUnknownCharacters];
NSString *originalString =[[NSString alloc] initWithData:base64Data encoding:NSUTF8StringEncoding];
NSLog(#"Result: %#",originalString);
Output:
You have to use an encoding that supports emoji and arabic characters. ASCII doesn't support that.
You should use NSUTF8StringEncoding everywhere, and you're fine.
Why are you using ASCII anyways? Why are you converting a string to an NSData and then back to NSString again? It doesn't make sense.

NSString iOS Russian encoding

I'm using code below:
AVMetadataItem *item = [self.player.metaData objectAtIndex:0];
NSLog("%#", item.stringValue);
Its works good with any english song title.
But, when i'm getting russian song title from AVMetadataItem:
ÐÐÐÐРÐЯ - ÐРСÐРÐÐÐТÐÐУ // СÐУШÐЮТ: 1585
How can i get something like:
Тратата - мы везем с собой кота.
Any help appreciated.
Try this:
NSData *test = [item.stringValue dataUsingEncoding:NSISOLatin1StringEncoding allowLossyConversion:YES];
NSString *dataString = [[NSString alloc] initWithData:test encoding:NSUTF8StringEncoding];
We get meta from audio in UTF8 and don't know in what NSStringEncoding it's converted so i use:
for (i = 0; i < 15; i++) {
NSData *test = [item.stringValue dataUsingEncoding:i allowLossyConversion:YES];
NSString *dataString = [[NSString alloc] initWithData:test encoding:NSUTF8StringEncoding];
}
Looks like iOS encoding Cyrillic(UTF8) in ISOLatin.

NSURL string content to a label

Im trying to read some values from a local url when i press a button and i send the value to a label. as soon as i launch the app and i press the button for the first time i get the value. but the second time i press the button the label get no text even though the NSLog message continues to display the value. any idea?
here is the code:
- (IBAction)refresh:(id)sender {
NSArray *listItems;
NSString *msg;
NSStringEncoding encoding;
NSURL *url = [NSURL URLWithString:#"http://192.168.1.177"];
NSData *data = [NSData dataWithContentsOfURL:url];
msg = [[NSString alloc] initWithData:data encoding:encoding];
listItems = [msg componentsSeparatedByString:#" "];
label.text = listItems[0];
NSLog(listItems[0]);
}
Your encoding variable is never initialized with a value, so it has the default value of 0, which is not a valid value of NSStringEncoding according to NSString.h. If you run the static analyzer (Command-Shift-B in Xcode), you will get a message that has an uninitialized value when it is used.
Change this line:
msg = [[NSString alloc] initWithData:data encoding:encoding];
to this:
msg = [[NSString alloc] initWithData:data encoding:NSUTF8StringEncoding];
(Substitute the appropriate encoding, but NSUTF8StringEncoding is a good first guess.)
You should also use a format string to log:
NSLog(#"%#", listItems[0]);

Convert NSString to UTF8String?

Currently I am download data from a server and I have this line to get that NSData into a NSString.
NSString *txt = [[NSString alloc] initWithData:data encoding:NSASCIIStringEncoding];
How would I go from here to convert that NSString into a UTF8 string?
Thanks!
NSData *utf8Data = [txt dataUsingEncoding:NSUTF8StringEncoding];
Change NSASCIIStringEncoding to NSUTF8StringEncoding .
I ran a quick test now with some dummy data and seemed to work fine with just that one line.

Resources