1

I am new in iOS and I am facing problem regarding to post string containing special character.

My code is like this in DidFinishLoading:

 NSXMLParser *myNSXMLParserPostObj=[[NSXMLParser alloc]initWithData:myNSMDataPostFromServer];
        myNSXMLParserPostObj.delegate=self;
        [myNSXMLParserPostObj parse];
        NSLog(@"%@",myNSXMLParserPostObj.parserError);
        NSString *responseStringWithEncoded = [[NSString alloc] initWithData: myNSMDataPostFromServer encoding:NSUTF8StringEncoding];
        //NSLog(@"Response from Server : %@", responseStringWithEncoded);
        NSAttributedString * attrStr = [[NSAttributedString alloc] initWithData:[responseStringWithEncoded dataUsingEncoding:NSUnicodeStringEncoding] options:@{ NSDocumentTypeDocumentAttribute: NSHTMLTextDocumentType } documentAttributes:nil error:nil];
        serverResponse.attributedText = attrStr;
        NSString *Str =serverResponse.text;
        NSLog(@"Server Response =%@",Str);
        UIAlertView *alert3 = [[UIAlertView alloc] initWithTitle:@"Success"
                                                         message:@"Complaint Added Successfully"
                                                        delegate:self
                                               cancelButtonTitle:@"OK"
                                               otherButtonTitles:nil];  

For getting data from web service I am using code like this:

loginStatusZone = [[NSString alloc] initWithBytes: [myNSMDatazoneFromServer mutableBytes] length:[myNSMDatazoneFromServer length] encoding:NSUTF8StringEncoding];
        NSLog(@"loginStatus =%@",loginStatusZone);
        NSError *parseError = nil;
        NSDictionary *xmlDictionary = [XMLReader dictionaryForXMLString:loginStatusZone error:&parseError];
        NSLog(@"JSON DICTIONARY = %@",xmlDictionary);
        recordResultZone = [xmlDictionary[@"success"] integerValue];
        NSLog(@"Success: %ld",(long)recordResultZone);
        NSDictionary* Address=[xmlDictionary objectForKey:@"soap:Envelope"];
        NSLog(@"Address Dict = %@",Address);
        NSDictionary *new =[Address objectForKey:@"soap:Body"];
        NSLog(@"NEW DICT =%@",new);
        NSDictionary *LoginResponse=[new objectForKey:@"FillZonesNewResponse"];
        NSLog(@"Login Response DICT =%@",LoginResponse);
        NSDictionary *LoginResult=[LoginResponse objectForKey:@"FillZonesNewResult"];
        NSLog(@"Login Result =%@",LoginResult);
        if(LoginResult.count>0)
        {
            NSLog(@"Login Result = %@",LoginResult);
            NSLog(@"Login Result Dict =%@",LoginResult);
            NSString *teststr =[[NSString alloc] init];
            teststr =[LoginResult objectForKey:@"text"];
            NSLog(@"Test String Value =%@",teststr);
            NSString *string = [LoginResult valueForKey:@"text"];

            NSData *data = [string dataUsingEncoding:NSUTF8StringEncoding];
            zoneresponsedict = [NSJSONSerialization JSONObjectWithData:data options:0 error:nil];
            idzonearray=[[NSMutableArray alloc]init];
            idzonearray=[zoneresponsedict valueForKey:@"ZoneId"];
            NSLog(@"Result Array =%@",idzonearray);
            shortnamezonearray=[[NSMutableArray alloc]init];
            shortnamezonearray=[zoneresponsedict valueForKey:@"ZoneName"];
            NSLog(@"Result Array =%@",shortnamezonearray);
        }

If I add special character then it shows me error as

Domain=NSXMLParserErrorDomain Code=111 "The operation couldn’t be completed. (NSXMLParserErrorDomain error 111.)" 

How can I post data with special character.

I am adding something like this in Thai language

enter image description here

But I am getting this:

enter image description here

Same with Hindi

enter image description here

Output is getting like this

enter image description here

But when anyone add Thai language using Android or website I get proper text in Thai. Only problem is with this post code.

halfer
  • 19,824
  • 17
  • 99
  • 186
Muju
  • 884
  • 20
  • 54
  • What is your special character, could you share (part of) your xml file with and without it? Error code 111 could indicate malformed xml file: http://stackoverflow.com/questions/20454853/nsxmlparsererrordomain-111 – koen Apr 21 '17 at 11:50
  • @Koen I am adding special character as "&<>". – Muju Apr 21 '17 at 11:53
  • See my answer below. – koen Apr 21 '17 at 11:57
  • @Koen No I directly need to pass it as it is. Because I also need to pass it in thai language.I do not want to replace string. – Muju Apr 21 '17 at 12:03
  • 1
    Please read this, maybe it is helpful: http://stackoverflow.com/questions/1091945/what-characters-do-i-need-to-escape-in-xml-documents – koen Apr 21 '17 at 12:08
  • @Koen its is not accepting &. – Muju Apr 21 '17 at 12:11
  • Did you try different string encodings, `NSUTF8StringEncoding` may not be the right one for Thai? – koen Apr 21 '17 at 12:14
  • @Koen which one I need to add. – Muju Apr 21 '17 at 12:16
  • @Koen I have add NSUTF32StringEncoding nothing happen. – Muju Apr 21 '17 at 12:21
  • Let us [continue this discussion in chat](http://chat.stackoverflow.com/rooms/142282/discussion-between-koen-and-muju). – koen Apr 21 '17 at 12:22

2 Answers2

2

You can replace your string so that it will allow you to send special character. You can use string method stringByReplacingOccurrencesOfString

 NSString *RaisedBy="Your Special character string";
        RaisedBy = [RaisedBy stringByReplacingOccurrencesOfString:@"&" withString:@"&amp;"];
        RaisedBy = [RaisedBy stringByReplacingOccurrencesOfString:@"<" withString:@"&lt;"];
        RaisedBy = [RaisedBy stringByReplacingOccurrencesOfString:@">" withString:@"&gt;"];
        RaisedBy = [RaisedBy stringByReplacingOccurrencesOfString:@"'" withString:@"&apos;"];
        RaisedBy = [RaisedBy stringByTrimmingCharactersInSet:[NSCharacterSet newlineCharacterSet]];

This code will also support thai language.

Mujtaba
  • 100
  • 10
1

You will need to write the &<> as follows, just like in an html file:

&amp; &lt; &gt;
koen
  • 5,383
  • 7
  • 50
  • 89