Rani
Rani

Reputation: 3453

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

I have an application where on a button click i am passing my server api method which calls the JSON post method and saves data to server database.Here i am saving my mobile number and emergency no to server database.My mobile number is in string format.In my mobile number string variable, my mobile number is getting saved which is in this format '+90-9491491411'.I am entering + and then code and then- and then number but when i send to the server database i am removing the - and sending the no to database but problem is in my server database + of mobile is not getting entered which i am entering .What may be the problem .I am using POST method to send the 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 emrgency number variable my no is in this format '+91986444711' but when the value is entered in server database + gets removed off .What may be the prob.

Upvotes: 7

Views: 6334

Answers (3)

mttrb
mttrb

Reputation: 8345

Unfortunately, NSString's -stringByAddingPercentEscapesUsingEncoding: will not convert the plus (+) sign into %2B because the plus sign is a valid URL character that is used to separate query parameters. What this usually means is that it gets converted to a space character by the web server.

The easiest way to replace the plus sign would be using NSString's stringByReplacingOccurrencesOfString:withString: to replace the + with %2B. For example:

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

Upvotes: 17

JeremyP
JeremyP

Reputation: 86651

The plus sign means space in a post request. You need to convert the plus to a percent escape character. The easiest way to do this is as follows:

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

This will turn the + into %2b. Probably the server will automatically reverse the encoding for you.

(Edited in line with mttrb's comment)

Upvotes: 0

MrTJ
MrTJ

Reputation: 13182

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

Upvotes: 1

Related Questions