应用程序在发送和接收特殊字符时崩溃

时间:2013-04-16 04:56:59

标签: iphone ios web-services ipad chat

我正在创建一个聊天应用程序。但是,现在我在发送特殊字符“&”时遇到了一个问题。我正在使用php web-services发送和接收消息。发送“&”字符,然后当某个人收到这个字符时,应用程序崩溃了。

1 个答案:

答案 0 :(得分:1)

在这里用它的代码替换那个字符如下...

当它在web服务中传递时,将其替换为如下所示..

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

然后当你想在那时接收它时用特殊字符替换..

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

UITextViewUITextField

中显示后

<强>更新

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

<强>更新

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

将字符串发送到web-serive时请使用bellow strign ..

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

并将此yourStringForWeb传递给网络服务...

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

string = [string stringByReplacingOccurrencesOfString:@"&amp;" 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 =&gt;

 ===>> Before ==>> Hello%20%26%20How%20Are%20You?
 ===>> After ==>> Hello & How Are You?