user2274239
user2274239

Reputation: 13

International Chars in UIAlertView on iOS

I'm having trouble displaying international chars in UIAlertView.

I'm retrieving strings in JSON format from this URL.

The returned format is ok and it contains the correct characters.

When I try to load it in my app in an UIAlertView I see this:

enter image description here

What is the right way to display Unicode characters from Spanish, Croatian, Chinese, etc?

Changing the default language in the settings did not solve the problem.

Upvotes: 0

Views: 176

Answers (1)

user352891
user352891

Reputation: 1191

Without any code, it's quite difficult to point out where you're going wrong. The code below works, I can only assume you're doing something particularly fruity with the encoded data.

NSData *data = [NSURLConnection sendSynchronousRequest:[NSURLRequest requestWithURL:[NSURL URLWithString:@"http://kancelarijahatipovic.com/translate.php?from=eng&dest=hr&phrase=iron"]] returningResponse:nil error:nil];    
NSString *string = [NSString stringWithUTF8String:[data bytes]];
SBJsonParser *parser = [[SBJsonParser alloc] init];
id returnVal = [parser objectWithString:string];  
if( [returnVal isKindOfClass:[NSDictionary class]]) {  
    //build string  
    NSMutableString *alertString = [NSMutableString string];    
    if( [returnVal objectForKey:@"tuc"] != nil ) {  
        NSArray *tucs = [returnVal objectForKey:@"tuc"];  

        for( id tucObject in tucs ) {  
            if( [tucObject isKindOfClass:[NSDictionary class]] && [tucObject objectForKey:@"phrase"] != nil ) {  
                id phraseObject = [tucObject objectForKey:@"phrase"];  

                if( [phraseObject isKindOfClass:[NSDictionary class]] && [phraseObject objectForKey:@"text"] != nil )  {  
                    [alertString appendFormat:@"%@\n", [phraseObject objectForKey:@"text"]];  
                }  
            }  
        }  
    }  

    if( alertString.length > 0 ) {  
        UIAlertView *alertView = [[UIAlertView alloc] initWithTitle:@"example" message:alertString delegate:nil cancelButtonTitle:@"okay" otherButtonTitles: nil];  
        [alertView show];  
    }  
}  

Edit: the SBJsonParser objectWithData: provides the same result as above.

Upvotes: 1

Related Questions