7. Show Arabic text on UILabel, UITextView, etc.

I got stuck for this requirement when I was getting response from server in two languages, English and Arabic, for English it was giving normal text but for Arabic, it was returning text something like this…

نريد أن نرى هذا في اللغة العربية

So then I figured out from ‘’ that these are code for arabic font and we have convert it into respective font strip using proper encoding, which is as follows:

int c = ... /* your 4 text digit unicode ordinal converted to an integer */
charString = [ NSString stringWithFormat:@"%C", c ];
//originally posted by hotpaw2 at this link.

But its just one char what if you want to show full sentence so I have made a small utility which will take arabic codes as input and will return string containing arabic font, as follows:

-(NSString*)toArabic:(NSString*) arabicCode
 NSArray *wordsArray = [arabicCode componentsSeparatedByString:@" "];
 NSString *sentence = @"";
 for(int i=0;i<[wordsArray count];i++)
 NSString *word = [wordsArray objectAtIndex:i];
 word = [word stringByReplacingOccurrencesOfString:@"&#" withString:@""];
 NSString *arabicForm = @"";
 NSArray *charArray = [word componentsSeparatedByString:@";"];
 for(int j=0;j<[charArray count];j++)

 int code = [[charArray objectAtIndex:j] intValue];

 arabicForm = [arabicForm stringByAppendingFormat:@"%C",code];

 sentence = [sentence stringByAppendingFormat:@" %@",arabicForm];

 return sentence;


How will it work?

NSString *arabicString = [self toArabic:@"&#1606;&#1585;&#1610;&#1583; 
&#1571;&#1606; &#1606;&#1585;&#1609;   &#1607;&#1584;&#1575; 
&#1601;&#1610;   &#1575;&#1604;&#1604;&#1594;&#1577;   
NSLog(@"Arabic text:%@",arabicString);

Hope it can help you somewhere.

Let me know for any confusion....

5. iPhone Server Communication

We can communicate in many ways with server. One way is using JSON, this is explained as below:

1. Starting Communication

[[[NSURLConnection alloc] initWithRequest:[NSURLRequest requestWithURL:
[NSURL URLWithString:urlStr]] delegate:self startImmediately:YES] autorelease];
//urlStr is NSString ref

2. Start receiving data

- (void)connection:(NSURLConnection *)connection didReceiveResponse:
   (NSURLResponse *)response{
    [receivedData setLength:0];
//receivedData is NSMutableData ref

3. Get data in chunks

- (void)connection:(NSURLConnection *)connection didReceiveData:(NSData *)data{
    [receivedData appendData:data];

4. After getting complete data

- (void)connectionDidFinishLoading:(NSURLConnection *)connection{

    // json data retrival complete    (received data is NSMutableData)
    if(receivedData == nil || [receivedData length] == 0){
      //handle error in getting response, may be no network
        [self showAlertViewWithTitle:@"No network" andMessage:@"Could not open the
        page, because internet connection was not found." andTag:0];

    NSString *responseBody = [[[NSString alloc] initWithData:receivedData
      encoding:NSUTF8StringEncoding] autorelease];
    NSError *jsonParsingError = [[[NSError alloc] init] autorelease];
    NSDictionary * jsonData = [NSDictionary dictionaryWithJSONData:
                                 [responseBody dataUsingEncoding:NSUTF8StringEncoding]

    NSDictionary *serverError = [jsonData objectForKey:@"error"]==[NSNull null]?
                                   nil:[jsonData objectForKey:@"error"];

    if(serverError==nil || [serverError count]==0){
        // no error found else
        NSDictionary *response = [jsonData objectForKey:@"response"]==[NSNull null]?
                                   nil:[jsonData objectForKey:@"response"];
        NSArray *array = [response objectForKey:@"array"]==[NSNull null]?nil:
                              [response objectForKey:@"array"];
// I have left the code incomplete, its just for understanding

5. Check for null values
Never forget to check null values from JSON data as follows:

NSString *dataValue =  [[[array objectAtIndex:i] objectForKey:@"key"]==[NSNull null]?
                         @"0":[[array objectAtIndex:i] objectForKey:@"key"] intValue]; 

Please let me know if you require further explanation..