0

I'm creating one chatting application.but,now I'm facing one problem while sending special character "&". I'm Using php web-services for sending and receiving the messages.while sending "&" character,and after that when some one received this character,the application is crashed.

Sonu
  • 937
  • 1
  • 10
  • 39

1 Answers1

1

here replace that character with its code like bellow...

when pass it in web-service at that time replace it like bellow..

yourString = [yourString stringByReplacingOccurrencesOfString:@"&" withString:@"&"];

and then when you want to receive it at that time replace with special character..

    yourString = [yourString stringByReplacingOccurrencesOfString:@"&" withString:@"&"];

after display it in UITextView or UITextField

UPDATE:

if (string2 != nil) {
   string1 = [NSString stringWithString:string2];
   NSString *strTemp = [NSString stringWithString:string2];
   NSLog(@"\n\n Temp String ==>> %@",strTemp);
}

UPDATE:

 NSString *yourString = @"Hello & How Are You?";
 yourString = [yourString stringByReplacingOccurrencesOfString:@"&" withString:@"&"];
NSLog(@"\n\n String Here => %@",yourString);

At sending string to web-serive use bellow strign..

NSString *yourStringForWeb = @"Hello & How Are You?";
 yourStringForWeb = [yourStringForWeb stringByReplacingOccurrencesOfString:@"&" withString:@"&"];

and pass this yourStringForWeb to the web-service...

NSString *string = @"Hello & How Are You?";

string = [string stringByReplacingOccurrencesOfString:@"&" withString:@"&"];
string = [string stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding];
string = [string stringByReplacingOccurrencesOfString:@"&" withString:@"%26"];
NSLog(@"\n\n ===>> Before ==>> %@",string);

NSString *string2 = string;

string2 = [string2 stringByReplacingOccurrencesOfString:@"%20" withString:@" "];
string2 = [string2 stringByReplacingOccurrencesOfString:@"%26" withString:@"&"];
NSLog(@"\n\n ===>> After ==>> %@",string2);

OutPut =>

 ===>> Before ==>> Hello%20%26%20How%20Are%20You?
 ===>> After ==>> Hello & How Are You?
Paras Joshi
  • 20,427
  • 11
  • 57
  • 70
  • i can send successfully from my device,but when i received than it's going to crash – Sonu Apr 16 '13 at 05:03
  • *** Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: '*** -[NSPlaceholderString initWithString:]: nil argument' *** First throw call stack: (0x2428012 0x1bafe7e 0x2427deb 0x206f25 0x206ea4 0xdb0ac 0xd6b05 0x6888fb 0x6889cf 0x6711bb 0x681b4b 0x61e2dd 0x1bc36b0 0x13a0fc0 0x139533c 0x1395150 0x13130bc 0x1314227 0x13148e2 0x23f0afe 0x23f0a3d 0x23ce7c2 0x23cdf44 0x23cde1b 0x27ae7e3 0x27ae668 0x5cdffc 0x297d 0x28b5) libc++abi.dylib: terminate called throwing an exception – Sonu Apr 16 '13 at 05:07
  • from which code and where it is crashed ?? actually from your this error here which string you assign to any variable or controls is nil.. so post that line where app crashed... – Paras Joshi Apr 16 '13 at 05:09
  • string1 = [NSString stringwithstrin string2]; – Sonu Apr 16 '13 at 05:12
  • also first print string2 with this code NSLog(@"\n\n string2 is ==>> %@",string2); – Paras Joshi Apr 16 '13 at 05:19
  • i m geeting null..and also can't send &. – Sonu Apr 16 '13 at 05:29
  • @Sweeta see this http://stackoverflow.com/questions/1105169/html-character-decoding-in-objective-c-cocoa-touch here you can see how to decode and encode the character and here you need to decode your string and send to server and after get it also encode it... – Paras Joshi Apr 16 '13 at 05:40
  • use this method -(NSString *)htmlEntityDecode:(NSString *)string { string = [string stringByReplacingOccurrencesOfString:@""" withString:@"\""]; string = [string stringByReplacingOccurrencesOfString:@"'" withString:@"'"]; string = [string stringByReplacingOccurrencesOfString:@"&" withString:@"&"]; string = [string stringByReplacingOccurrencesOfString:@"<" withString:@"<"]; string = [string stringByReplacingOccurrencesOfString:@">" withString:@">"]; return string; } – Paras Joshi Apr 16 '13 at 05:41
  • i do not get any sentence after having & – Sonu Apr 18 '13 at 06:55
  • @Sweeta i updated my answert just take my code and run in your project check what you will get.. :) i got String Here => Hello & How Are You? – Paras Joshi Apr 18 '13 at 07:02
  • ohh yeah,but now i m sending this string using web-service. – Sonu Apr 18 '13 at 07:06
  • and i did'nt get same string. – Sonu Apr 18 '13 at 07:07
  • what u mean?? means you want to sedn this string to web-service?? – Paras Joshi Apr 18 '13 at 07:07
  • yeah.i m sending this string to web service – Sonu Apr 18 '13 at 07:09
  • @Sweeta see above i update answer... when you want to pass that string the web then use second option and when you want to display that string in UITextView the first option... – Paras Joshi Apr 18 '13 at 07:09
  • this is my code when i m sending text strResult=[strResult stringByReplacingOccurrencesOfString: @"&" withString: @"&"]; – Sonu Apr 18 '13 at 07:13
  • and when i received string i use this code strUpdate = [ strUpdate stringByReplacingOccurrencesOfString:@"&" withString: @"&" ]; – Sonu Apr 18 '13 at 07:14
  • the output is %20how%20Are%20You? still missing hello – Sonu Apr 18 '13 at 07:31
  • @Sweeta dear i tested now see my whole code which i test .. out put is.. ===>> Before ==>> Hello%20%26%20How%20Are%20You? 2013-04-18 13:05:57.244 iAppoint[1082:13d03] ===>> After ==>> Hello & How Are You? – Paras Joshi Apr 18 '13 at 07:36
  • yes this is unicode for that chracter and also same as %20 is code for SPACE " " – Paras Joshi Apr 18 '13 at 08:12
  • still i m getting text which is conti..after & – Sonu Apr 18 '13 at 08:38
  • what?? not understand dear?? :( – Paras Joshi Apr 18 '13 at 08:40
  • hello & how are you.i m passing this string – Sonu Apr 18 '13 at 08:41
  • how are you i m getting this string – Sonu Apr 18 '13 at 08:42
  • you tried my above code??? its give perfect output which you want.. its right dude.. :) – Paras Joshi Apr 18 '13 at 08:43
  • now I'm passing hello %26 how are you? and output us hello %2526 how are you? – Sonu Apr 18 '13 at 09:21
  • hey dont pass this type of string NSString *string = @"Hello %26 How Are You?"; here we encode that string and then set %26 so dont write string with its unicode write like this NSString *string = @"Hello & How Are You?"; – Paras Joshi Apr 18 '13 at 10:02
  • yeah,i understud.but my requirement is hello % how are you? and i get hello %25 how are you? %25 is unicode for %.but i want same response which i send to service. – Sonu Apr 18 '13 at 10:30
  • let us [continue this discussion in chat](http://chat.stackoverflow.com/rooms/28433/discussion-between-paras-joshi-and-sweeta) – Paras Joshi Apr 18 '13 at 10:33
  • @Sweeta please accept this answer and also another from this link http://stackoverflow.com/questions/16081390/how-to-get-from-web-service-in-iphone/16081968#16081968 .. thanx.. – Paras Joshi Apr 18 '13 at 12:23