views:

2140

answers:

3

I'm trying to send the contents of UITextView or UITextField as parameters to a php file

NSString *urlstr = [[NSString alloc] initWithFormat:@"http://server.com/file.php?name=%@&tags=%@&entry=%@",nameField.text, tagsField.text, dreamEntry.text];

When i log urlstr, the url format is ok just as long as the UITextView or UITextField don't contain spaces. How would i go about converting the spaces to %20 ?

edit

here is the code at present, which not only crashes but isn't encoding the url properly.

name=John Doe&tags=recurring nightmare&entry=Testing testing testing

is converted to

name=John -1844684964oe&tags=recurringightmare&entry=Testing 4.214929e-307sting -1.992836e+00sting

- (IBAction)sendButtonPressed:(id)sender
{

    NSString *urlString = [[NSString alloc] initWithFormat:@"http://server.com/file.php?name=%@&tags=%@&entry=%@", nameField.text, tagsField.text, dreamEntry.text];

    NSString *encodedString = [urlString stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding];

    NSURL *url = [[NSURL alloc] initWithString:encodedString];

    NSLog(encodedString);

    NSLog(urlString);

    [urlString release];
    [url release];
    [encodedString release];


}
A: 

You can take your URL and use:

NSString *urlStr = [[NSString alloc] initWithFormat:@"http://server.com/file.php?name=%@&tags=%@&entry=%@",nameField.text, tagsField.text, dreamEntry.text];

NSString *encStr = [urlStr stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding];
Heat Miser
thanks for the pointer :)
Bernardo Moreira
NP... the above answer is indeed more correct. Thx...
Heat Miser
+4  A: 

You're not supposed to URL-escape the entire string, you're supposed to URL-escape the dynamic components. Try

NSString *urlStr = [NSString stringWithFormat:@"http://server.com/file.php?name=%@&tags=%@&entry=%@",
                        [nameField.text stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding],
                        [tagsField.text stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding],
                        [dreamEntry.text stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding],
                        nil];
NSURL *url = [NSURL URLWithString:urlStr];

The second issue with your code (and undoubtedly the reason for the odd printing) is you're passing the string directly to NSLog, so it's being treated as a format string. You need to use

NSLog(@"%@", encodedString);

instead. That will make it print as expected.

Edit: A third issue with your code is you're mixing autoreleased and owned objects, then releasing them all at the end. Go look at the 3 objects you create, and which you subsequently release later. One of them shouldn't be released later because it was produced by a method that did not start with the words alloc, copy, or new. Identifying the object in question is an exercise left to the reader.

Kevin Ballard
hehe point taken. thanks!
Bernardo Moreira
A: 

this works:

- (IBAction)sendButtonPressed:(id)sender
{


    NSString *url = [NSString stringWithFormat:@"http://www.server.com/file.php?name=%@&tags=%@&entry=%@",
         [nameField.text stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding],
         [tagsField.text stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding],
         [dreamEntry.text stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding],
         nil];

    NSLog(@"%@", url);

}
Bernardo Moreira