Question

I would like to pass multiple parameters from the iphone sdk to a server-side php which interfaces with a mySQL database.

i found some answers on how to do this, but i'm having a hard time figuring out how to include several parameters.

what i have right now is


- (IBAction)sendButtonPressed:(id)sender
{
    NSString *urlstr = [[NSString alloc] initWithFormat:@"http://server.com/file.php?date=%d", theDate];
    NSURL *url = [[NSURL alloc] initWithString:urlstr];

    [urlstr release];
    [url release];
}

which would work for 1 parameter, but what i'm looking for would be something like

http://server.com/file.php?date=value&time=value&category=value&tags=value&entry=value

how would i going about doing this?

Was it helpful?

Solution

The - initWithFormat method takes multiple arguments for the format string.

So you can do things like this:

  NSString *urlstr = [[NSString alloc] initWithFormat:@"http://server.com/file.php?date=%d&second=%d&third=%d", theDate, 2, thirdIVar];

- initWithFormat works almost identically to printf() and it variants.

Here is some printf() examples http://stahlforce.com/dev/index.php?tool=csc02

Edit: Where are the variables nameField, tagsField, dreamEntry defined and set?

Unless they are NSStrings and defined in the @interface you can not uses them in this way.

I suggest hard coding some values for testing:

    NSString *urlstr = [[NSString alloc] initWithFormat:@"http://server.com/file.php?date=%@&time=%@&name=%@&category=%d&tags=%@&entry=%@", nil, nil, @"Name", nil, @"Tags", @"Dream"];

OTHER TIPS

Creating an NSURL doesn't open communications with a server. It's just a data structure for holding a URL. You want to read up on NSURLConnection.

Are all the variables you're passing in your format really numbers? %d is the placeholder for a number; %@ is an object. It's very surprising to be passing nil if you're expecting a number, even for testing purposes. It'll "work" because nil is 0, but it suggests that these aren't really numbers.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top