Adding + before using% 2b using url encoding in iphone

I have an application where, when I click a button, I pass my api method to the server, which calls the JSON mailing method and stores the data in the server database. Here I save the mobile number and emergency assistance is not in the server database. My mobile number is in string format. In my mobile number variable, my mobile number gets saved, which is in this format "+ 90-9491491411". I enter +, and then the code, and then - and then the number, but when I send to the server database I delete - and sends no to the database, but the problem in my server + mobile database is not entered, which I enter. What could be the problem. I use the POST method to send a request. This is my code.

-(void)sendRequest { NSString *newstring = txtMobile.text; mobileValue = [newstring stringByReplacingOccurrencesOfString:@"-" withString:@""]; NSLog(@"%@",mobileValue); NSString *newString1 = txtemergencyprovider.text; emergencyNumber = [newString1 stringByReplacingOccurrencesOfString:@"-" withString:@""]; NSLog(@"%@",emergencyNumber); if ([txtEmail.text isEqualToString:@""]) { post = [NSString stringWithFormat:@"CommandType=new&ApplicationType=%d&FullName=%@&Mobile=%@&EmergencymobileNumber=%@&Latitude=%f&Longitude=%f&City=%@&MobileModel=Apple",applicationtype,txtFullname.text,mobileValue,emergencyNumber,latitude,longitude,txtCity.text]; NSLog(@"%@",post); } else { post = [NSString stringWithFormat:@"CommandType=new&ApplicationType=%d&FullName=%@&Mobile=%@&EmergencymobileNumber=%@&Latitude=%f&Longitude=%f&City=%@&EmailAddress=%@&MobileModel=Apple",applicationtype,txtFullname.text,mobileValue,emergencyNumber,latitude,longitude,txtCity.text,txtEmail.text]; NSLog(@"%@",post); } NSData *postData = [post dataUsingEncoding:NSASCIIStringEncoding allowLossyConversion:YES]; NSString *postLength = [NSString stringWithFormat:@"%d", [postData length]]; NSLog(@"%@",postLength); NSMutableURLRequest *request = [[NSMutableURLRequest alloc] init]; [request setURL:[NSURL URLWithString:@"http://myapi?RequestType=NEW"]]; [request setHTTPMethod:@"POST"]; [request setValue:postLength forHTTPHeaderField:@"Content-Length"]; [request setValue:@"application/x-www-form-urlencoded" forHTTPHeaderField:@"Content-Type"]; [request setHTTPBody:postData]; NSURLConnection *theConnection = [[NSURLConnection alloc] initWithRequest:request delegate:self]; if (theConnection) { webData = [[NSMutableData data] retain]; NSLog(@"%@",webData); } else { } } 

// In my mobile number and variable number, emhgency my no is in this format "+91986444711", but when the value is entered into the server + database, it is deleted. What could be the problem.

+7
source share
3 answers

Unfortunately, NSString -stringByAddingPercentEscapesUsingEncoding: does not convert the plus sign ( + ) to %2B because the plus sign is a valid URL character, which is used to separate request parameters. This usually means that the web server is converted to a space character.

The easiest way to replace the plus sign is to use NSString stringByReplacingOccurrencesOfString:withString: to replace + with %2B . For example:

 mobileValue = [mobileValue stringByReplacingOccurrencesOfString:@"+" withString:@"%2B"]; 
+16
source

The plus sign ("+") in the URL means encoded space ("), and most likely your server will interpret it as a space. Change the plus sign to% 2B in the line before sending. For the complete solution for encoding URLs, see this post: http://madebymany.com/blog/url-encoding-an-nsstring-on-ios

+1
source

A plus sign indicates a space in the mail request. You need to convert the plus value to the mileage symbol. The easiest way to do this:

 NSString* escapedMobileValue = [mobileValue stringByReplacingOccurencesOfString: @"+" withString: @"%2b"]; 

This will turn + into %2b . The server will probably automatically change the encoding for you.

(Edited according to mttrb comment)

0
source

All Articles