0

I am trying to make a url by first collecting the parameters, and then in one statement creating the actual url. Here is what I am trying to do:

NSString *urlString = @"http://www.some_login_url.com?email=%@&password=%@";

NSString *email = self.email.text;
NSString *password = self.password.text;

NSString *url_to_send = [NSString stringWithFormat:@"%@%@", urlString , email , password];     

So what I wanted to do was replace the @ symbols with the values in the variables, but instead the second variable just got appended to the end of the string.

How would I change the last line so I could put the right parameters in their correct spots?

Thanks!!

GeekedOut
  • 16,905
  • 37
  • 107
  • 185

2 Answers2

1

I think you meant:

NSString *url_to_send = [NSString stringWithFormat:urlString , email , password];     

But you might want to look for ways to construct a URL that do proper escaping of arguments, etc. For example, see this question.

Community
  • 1
  • 1
Jesse Rusak
  • 56,530
  • 12
  • 101
  • 102
  • do you mean that line where they have the NSString *value = [(NSString*)CFURLCreateStringByAddingPercentEscapes(kCFAllocatorDefault, (CFStringRef)[args objectForKey:key], NULL, CFSTR("/?&:=#"), kCFStringEncodingUTF8) autorelease]; – GeekedOut Jul 07 '12 at 15:20
  • Yes, or either of the other two answers which suggest libraries that do this for you (and handle other issues like adding the ? and & characters as needed, etc.) – Jesse Rusak Jul 07 '12 at 15:21
  • if I am just sending the url, is handling the & and the ? characters that important? In the example in this comment, its a bit crypitic for me to read still....which is the actual variable that is used? Any chance you could post how mine should look like? THanks! – GeekedOut Jul 07 '12 at 15:23
  • btw I am also sending a password field. I was wondering if there is something else other than a secure connection that needs to be done for that field. – GeekedOut Jul 07 '12 at 15:24
  • You might want to search around or post another question about the security issue, but yes, you should at least be using HTTPS if you're sending a clear-text password. As for the & and ? characters, if you add escaping code like you posted above, that'll be fine. I just meant that those libraries will add them in for you between parameters as needed instead of having to hard-code them yourself. – Jesse Rusak Jul 07 '12 at 15:34
1

The urlString is your format, so you want:

NSString *url_to_send = [NSString stringWithFormat:urlString , email , password];
MattR
  • 6,908
  • 2
  • 21
  • 30