Search code examples
objective-cnsstringnsdatahttp-request

How to define my string and type of encoding. NSString -NSSdata


I am trying to post a string with 2 values to a URL. To define my string I am using:

NSString *post = [NSString stringWithFormat:@"...."];
NSData *postData = [post dataUsingEncoding: .... allowLossyConversion: NO];

The string I want to post is this one: geoX#value #geoY#value.
How do I add this string in my NSString command and what type of dataUsingEncoding should I use?

Also at the command:

[request setValue:@"....." forHTTPHeaderField:"...."]

What value do I add and what type of content if header -->content-type :text/html?


Solution

  • Assuming that you have the values stored in two variables as follows,

    NSString *geoXValue = @"1.2343243";
    NSString *geoYValue = @"1.5646546";
    

    This is how you need to append the values to string,

    NSString *post = [NSString stringWithFormat:@"geoX%@ geoY%@", geoXValue, geoYValue];
    

    To convert this to data, you can use the following line,

    NSData *postData = [post dataUsingEncoding:NSUTF8StringEncoding allowLossyConversion:NO];
    

    And to set the content-type in header, try this,

    [request setValue:@"text/html" forHTTPHeaderField:"Content-Type"]
    

    Hope it is clear now.