From b5b69385c310cf4ab797f7007896b88e977d862a Mon Sep 17 00:00:00 2001 From: Joao Caixinha Date: Fri, 2 Oct 2015 17:14:19 +0100 Subject: [PATCH 1/2] Implements #3 emoji --- Pod/Classes/OrtcClient/OrtcClient.m | 44 +++++++++++++++++-- .../OrtcClient/RealtimePushAppDelegate.h | 1 - 2 files changed, 41 insertions(+), 4 deletions(-) diff --git a/Pod/Classes/OrtcClient/OrtcClient.m b/Pod/Classes/OrtcClient/OrtcClient.m index 34b9a60..3b2927b 100644 --- a/Pod/Classes/OrtcClient/OrtcClient.m +++ b/Pod/Classes/OrtcClient/OrtcClient.m @@ -1278,7 +1278,13 @@ - (void)opReceive:(NSString*) message { [msgSentDict setObject:[NSNumber numberWithBool:YES] forKey:@"isMsgSent"]; [messagesBuffer setObject:msgSentDict forKey:messageId]; } + + aMessage = [self escapeRecvChars:aMessage]; + + NSData *pos = [aMessage dataUsingEncoding:NSUTF8StringEncoding]; + aMessage = [[NSString alloc] initWithData:pos encoding:NSNonLossyASCIIStringEncoding]; + channelSubscription.onMessage(self, aChannel, aMessage); } } @@ -1295,12 +1301,14 @@ - (NSString*)simulateJsonParse:(NSString*) str{ NSMutableString *ms = [NSMutableString string]; for(int i =0; i < [str length]; i++){ unichar ascii = [str characterAtIndex:i]; + if(ascii > 128){ //unicode [ms appendFormat:@"%@", [NSString stringWithCharacters:&ascii length:1]]; - } else { //ascii + }else { //ascii if(ascii == '\\'){ i = i + 1; int next = [str characterAtIndex:i]; + if(next == '\\'){ [ms appendString:@"\\"]; } else if(next == 'n'){ @@ -1315,15 +1323,45 @@ - (NSString*)simulateJsonParse:(NSString*) str{ [ms appendString:@"\r"]; } else if(next == 't'){ [ms appendString:@"\t"]; - } + } else if(next == 'u'){ + [ms appendString:@"\\u"]; + } } else { [ms appendFormat:@"%c", ascii]; } } } - return ms; + + + + + return ms ; } + +- (NSString *) escapedUnicode:(NSString*)str +{ + NSMutableString *uniString = [ [ NSMutableString alloc ] init ]; + UniChar *uniBuffer = (UniChar *) malloc ( sizeof(UniChar) * [ str length ] ); + CFRange stringRange = CFRangeMake ( 0, [ str length ] ); + + CFStringGetCharacters ( (CFStringRef)str, stringRange, uniBuffer ); + + for ( int i = 0; i < [ str length ]; i++ ) { + if ( uniBuffer[i] > 0x7e ) + [ uniString appendFormat: @"\\u%04x", uniBuffer[i] ]; + else + [ uniString appendFormat: @"%c", uniBuffer[i] ]; + } + + free ( uniBuffer ); + + NSString *retString = [ NSString stringWithString: uniString ]; + + return retString; +} + + - (NSString*)generateId:(int) size { CFUUIDRef uuidRef = CFUUIDCreate(NULL); diff --git a/Pod/Classes/OrtcClient/RealtimePushAppDelegate.h b/Pod/Classes/OrtcClient/RealtimePushAppDelegate.h index 253578f..b8b1c40 100644 --- a/Pod/Classes/OrtcClient/RealtimePushAppDelegate.h +++ b/Pod/Classes/OrtcClient/RealtimePushAppDelegate.h @@ -5,7 +5,6 @@ // Created by iOSdev on 9/20/13. // Copyright (c) 2013 Realtime.co All rights reserved. // - #import #define NOTIFICATIONS_KEY @"Local_Storage_Notifications" From cd5268d7adac085f35de93dc5eb048226b2df692 Mon Sep 17 00:00:00 2001 From: Joao Caixinha Date: Mon, 5 Oct 2015 14:47:10 +0100 Subject: [PATCH 2/2] emoji with other unicode chars --- Pod/Classes/OrtcClient/OrtcClient.m | 53 +++++++++++++---------------- 1 file changed, 24 insertions(+), 29 deletions(-) diff --git a/Pod/Classes/OrtcClient/OrtcClient.m b/Pod/Classes/OrtcClient/OrtcClient.m index 3b2927b..78f1e6c 100644 --- a/Pod/Classes/OrtcClient/OrtcClient.m +++ b/Pod/Classes/OrtcClient/OrtcClient.m @@ -1279,12 +1279,9 @@ - (void)opReceive:(NSString*) message { [messagesBuffer setObject:msgSentDict forKey:messageId]; } - aMessage = [self escapeRecvChars:aMessage]; - NSData *pos = [aMessage dataUsingEncoding:NSUTF8StringEncoding]; - aMessage = [[NSString alloc] initWithData:pos encoding:NSNonLossyASCIIStringEncoding]; - + aMessage = [self checkForEmoji:aMessage]; channelSubscription.onMessage(self, aChannel, aMessage); } } @@ -1292,12 +1289,34 @@ - (void)opReceive:(NSString*) message { } } + +- (NSString*)checkForEmoji:(NSString*)str{ + for (int i = 0; i < str.length; i++) { + unichar ascii = [str characterAtIndex:i]; + if(ascii == '\\'){ + i = i + 1; + int next = [str characterAtIndex:i]; + + if(next == 'u'){ + NSString *emoji = [str substringWithRange:NSMakeRange(i - 1, 12)]; + NSData *pos = [emoji dataUsingEncoding:NSUTF8StringEncoding]; + emoji = [[NSString alloc] initWithData:pos encoding:NSNonLossyASCIIStringEncoding]; + + str = [str stringByReplacingCharactersInRange:NSMakeRange(i - 1, 12) withString:emoji]; + } + } + } + return str; +} + + - (NSString*)escapeRecvChars:(NSString*) str{ str = [self simulateJsonParse:str]; str = [self simulateJsonParse:str]; return str; } -- (NSString*)simulateJsonParse:(NSString*) str{ + +- (NSString*)simulateJsonParse:(NSString*)str{ NSMutableString *ms = [NSMutableString string]; for(int i =0; i < [str length]; i++){ unichar ascii = [str characterAtIndex:i]; @@ -1334,34 +1353,10 @@ - (NSString*)simulateJsonParse:(NSString*) str{ - return ms ; } -- (NSString *) escapedUnicode:(NSString*)str -{ - NSMutableString *uniString = [ [ NSMutableString alloc ] init ]; - UniChar *uniBuffer = (UniChar *) malloc ( sizeof(UniChar) * [ str length ] ); - CFRange stringRange = CFRangeMake ( 0, [ str length ] ); - - CFStringGetCharacters ( (CFStringRef)str, stringRange, uniBuffer ); - - for ( int i = 0; i < [ str length ]; i++ ) { - if ( uniBuffer[i] > 0x7e ) - [ uniString appendFormat: @"\\u%04x", uniBuffer[i] ]; - else - [ uniString appendFormat: @"%c", uniBuffer[i] ]; - } - - free ( uniBuffer ); - - NSString *retString = [ NSString stringWithString: uniString ]; - - return retString; -} - - - (NSString*)generateId:(int) size { CFUUIDRef uuidRef = CFUUIDCreate(NULL);