javascriptobjective-cstringescapeutils

vietnamese text send to javascript from objective c gets distorted


I am sending a json string from Objective C to javascript code. But the text string gets distorted

{"data":{"type":"new","id":"xyz","text":"Thêm nhóm ês","userid":["16"]},"type":"hello","action":"update"}

The vietnamese string Thêm nhóm ês gets changed

How i achieve this :

@try
    {
        NSData *utf8Data = [data dataUsingEncoding:NSUTF8StringEncoding];
        NSString *encodedString = [utf8Data base64EncodedStringWithOptions:0];

        NSString *jsString = [NSString stringWithFormat:@"javascript:notifyJSObservers('%@','%@')",eventName, encodedString];

        [self.webView stringByEvaluatingJavaScriptFromString:jsString];
    }
    @catch (NSException *exception)
    {
        NSLog(@"JS issues :: %@",exception.reason);
    }

Can anybody throw some light on this.


Solution

  •  @try
        {
            NSData   *nsdata        = [data dataUsingEncoding:NSUTF8StringEncoding];
            NSString *base64Encoded = [nsdata base64EncodedStringWithOptions:0];
            int code = 1004;
            NSString *jsString = [NSString stringWithFormat:@"javascript:handleResponseFromNative('%d','%@')",code, base64Encoded];
    
            [self.previewWebView performSelector:@selector(stringByEvaluatingJavaScriptFromString:) withObject:jsString afterDelay:0.5];
        }
        @catch (NSException *exception)
        {
            NSLog(@"JS issues :: %@",exception.reason);
        }