NSString iOS Russian encoding - ios

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.

Related

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.

Song title by stream wrong Encoding

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

Bullet points are not recognized in HTML String

I have a string:
NSString *str1 = #"\u2022 You were held in custody for a longer
period of time than may have been necessary.";
I am converting it into an HTML string using this code :
- (NSString *)HTMLString {
NSDictionary * const exportParams = #{NSDocumentTypeDocumentAttribute: NSHTMLTextDocumentType};
NSAttributedString *attributed = [[NSAttributedString alloc] initWithString:str1];
NSData *htmlData = [attributed dataFromRange:NSMakeRange(0, attributed.length) documentAttributes:exportParams error:nil];
return [[NSString alloc] initWithData:htmlData encoding:NSUTF8StringEncoding];
}
But bullet points are not showing in the HTML string. It's showing a ? instead of bullet point. Please tell me any solution.
Please try below code -
- (NSString *)HTMLString {
NSDictionary * const exportParams = #{NSDocumentTypeDocumentAttribute: NSHTMLTextDocumentType};
letterString = [letterString stringByReplacingOccurrencesOfString:#"\u2022" withString:#"•"];
NSAttributedString *attributed = [[NSAttributedString alloc] initWithString:letterString];
NSData *htmlData = [attributed dataFromRange:NSMakeRange(0, attributed.length) documentAttributes:exportParams error:nil];
return [[NSString alloc] initWithData:htmlData encoding:NSUTF8StringEncoding];
}

EMoji Unicode for IOS

I call api and get the string which include the android java unicode in NSDictionary :
\ud83d\ude25\ud83d\ude1e\U2764\ud83d\ude2d
When get the the value above and assign to string, I just will get the string as:
\ud83d\ude25\ud83d\ude1e❤\ud83d\ude2d
I totally can not get the value \U2764 but get the value ❤
So I can not replace the code \U2764 to IOS unicode.
How can I get it \U2764? I convert into UTF-8.
Thanks
Edit:
This is get the directory from api (dic):
{
message = "\ud83d\ude25\ud83d\ude1e\U2764\ud83d\ude2d";
mesgid = "213133";
Thumbnail = "";
}
Then i get the message, but the dic2 string is still get \ud83d\ude25\ud83d\ude1e❤\ud83d\ude2d:
const char *cString = [[dic6 objectForKey:#"Message"] UTF8String];
NSString *dic2 = [[NSString alloc] initWithUTF8String:cString];
if ([dic2 rangeOfString:#"\\U"].location==NSNotFound && [dic2 rangeOfString:#"\\u"].location==NSNotFound)
{
NSLog(#"Substring Not Found");
}
else
{
NSLog(#"Substring Found");
}
I totally can not get the unicode.
Why?
try this
NSString *uniString = [NSString stringWithUTF8String:#"\ud83d\ude25\ud83d\ude1e❤\ud83d\ude2d
"];
Hopefully this will resolve your problem
char cString[] = "\ud83d\ude25\ud83d\ude1e\U2764\ud83d\ude2d";
NSData *data = [NSData dataWithBytes:cString length:strlen(cString)];
NSString *string = [[NSString alloc] initWithData:data encoding:NSUTF8StringEncoding];

String printing incorrectly in table view after JSON parsing

I one of my apps, i parse some data from local host and print it in a table view. To get the data, the user first logs in using an alert view. The user id entered is then used to fetch the data which i parse using JSON.
There is definitely a very simple solution to this question but I can't seem to be able to fix it. The problem is that when I print the data the string comes in this format:
( "string" )
But I want it so that it just says : string
in the table view. Here is my parsing method:
- (void)updateMyBooks
{
dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^{
// Fetch data on a background thread:
NSString *authFormatString =
#"http://localhost:8888/Jineel_lib/bookBorrowed.php?uid=%#";
NSString *string = [[NSString alloc]initWithFormat:#"%#",UserID];
NSString *urlString = [NSString stringWithFormat:authFormatString, string];
NSURL *url = [NSURL URLWithString:urlString];
NSLog(#"uel is %#", url);
NSString *contents = [NSString stringWithContentsOfURL:url encoding:NSUTF8StringEncoding error:nil];
response1 = [contents JSONValue];
if (contents) {
// ... Parse JSON response and add objects to newBooksBorrowed ...
BookName = [[NSString alloc]init];
DateBorrowed = [[NSString alloc]init];
BookID = [[NSString alloc]init];
BookExtended = [[NSString alloc]init];
BookReturned = [[NSString alloc]init];
BookName = [response1 valueForKey:#"BookName"];
BookID = [response1 valueForKey:#"BookID"];
DateBorrowed = [response1 valueForKey:#"DateBorrowed"];
BookExtended = [response1 valueForKey:#"Extended"];
BookReturned = [response1 valueForKey:#"Returned"];
dispatch_sync(dispatch_get_main_queue(), ^{
// Update data source array and reload table view.
[BooksBorrowed addObject:BookName];
NSLog(#"bookBorrowed array = %#",BooksBorrowed);
[self.tableView reloadData];
});
}
});
}
This is how I print it in the table view:
NSString *string = [[NSString alloc] initWithFormat:#"%#",[BooksBorrowed objectAtIndex:indexPath.row]];
NSLog(#"string is %#",string);
cell.textLabel.text = string;
When I use log during the parsing process, it comes out as ( "string" ) so the problem is somewhere in the parsing, at least thats what I think.
If
NSString *string = [[NSString alloc] initWithFormat:#"%#",[BooksBorrowed objectAtIndex:indexPath.row]];
returns something like "( string )" then the most probably reason is that
[BooksBorrowed objectAtIndex:indexPath.row]
is not a string, but an array containing a string. In that case,
NSString *string = [[BooksBorrowed objectAtIndex:indexPath.row] objectAtIndex:0];
should be the solution.
NSString *string = [[NSString alloc] initWithFormat:#"%#",[BooksBorrowed objectAtIndex:indexPath.row]];
string = [string stringByReplacingOccurrencesOfString:#"(" withString:#""];
string = [string stringByReplacingOccurrencesOfString:#")" withString:#""];
string = [string stringByReplacingOccurrencesOfString:#"\"" withString:#""];
NSLog(#"string is %#",string);
cell.textLabel.text = string;
EDIT:
Above code is used if it's showing text in that format in your label.
If you see that in NSlog then it's NSString inside NSArray.
You need to fetch that string first from array and then display, use code line suggested by #Martin R for that.
NSString *string = [[BooksBorrowed objectAtIndex:indexPath.row] objectAtIndex:0];

Resources