delete
delete

Reputation: 19158

NSString: dealing with UTF8-based API

Which characterset is the default characterset for NSString, when i get typed content from a UITextField?

I developed an app, which sends such NSStrings to a UTF8-based REST-API. At the backend, there is an utf8 based MySQL-Database and also utf8-based varchar-fields. My POST-Request sends string data from the iOS App to the server. And with a GET-Request i receive those strings from the REST API.

Within the App, everything is printed fine. Special UTF-8-Characters like ÄÖÜ are showed correctly after sending them to the server and after receive them back.

But when i enter the mysql-console of the server of the REST API, and do a SELECT-Command at these data, there are broken characters visible.

What could be the root cause? In which characterset does Apple use a NSString?

Upvotes: 0

Views: 654

Answers (2)

Rick James
Rick James

Reputation: 142366

If, by "mysql console", you are referring to the DOS-like window in Windows, then you need:

The command "chcp" controls the "code page". chcp 65001 provides utf8, but it needs a special charset installed, too. some code pages To set the font in the console window: Right-click on the title of the window → Properties → Font → pick Lucida Console

Also, tell the 'console' that your bytes are UTF8 by doing SET NAMES utf8mb4.

Upvotes: 0

zaph
zaph

Reputation: 112855

It sounds like it is a server issue. Check that the version you are using supports UTF-8, older versions do not. See : How to support full Unicode in MySQL database

MySQL’s utf8 encoding is different from proper UTF-8 encoding. It doesn’t offer full Unicode support.

MySQL 5.5.3 (released in early 2010) introduced a new encoding called utf8mb4 which maps to proper UTF-8 and thus fully supports Unicode.

NSString has in internal representation that is essentially opaque.

The UITextField method text returns an NSString.

When you want data from a string use to send to a server use - (NSData *)dataUsingEncoding:(NSStringEncoding)encoding and specify the encoding such as NSUTF8StringEncoding.

NSData *textFieldUTF8Data = [textFieldInstance.text dataUsingEncoding: NSUTF8StringEncoding];

Upvotes: 3

Related Questions