Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How can pass multiple parameters in NSURL string in iOS?

I am developing one app in that app I need pass more than one parameters at a time in NSURL my code is

responseData = [[NSMutableData data] retain];
ArrData = [NSMutableArray array];
NSURL *url = [NSURL URLWithString:[NSString stringWithFormat:@"http://rate-exchange.appspot.com/currency?from=%@&to=%@&q=%@",strfrom,strto,strgo]];
NSURLRequest *request = [NSURLRequest requestWithURL:[NSURL URLWithString:url]];
//NSURLRequest *request1 = [NSURLRequest requestWithURL:
//[NSURL URLWithString:@"http://rate-exchange.appspot.com/currency?from=%@&to=%@&q=1",strfrom,strto]];

the above code I need to pass more than one parameter Dynamically. is it possible ? if it is, then how? thanks & regards

like image 994
areddy Avatar asked Feb 26 '13 12:02

areddy


2 Answers

try creating a separate string before adding to the URL something like

 NSSString *strURL=[NSString stringWithFormat:@"http://rate-exchange.appspot.com/currency?from=%@&to=%@&q=%@"‌​,strfrom,strto,strgo];

and then add this strURL to URL

NSURL *url = [NSURL URLWithString:strURL];

finally add it to the request, your code is wrong where you adding url to request, URL is not a string it is a URL so it should be requestWithURL not URLWithString, it should be like this

NSURLRequest *request = [NSURLRequest requestWithURL:url];
like image 98
nsgulliver Avatar answered Oct 15 '22 07:10

nsgulliver


One thing many of these answers is missing is the use of [NSString stringByAddingPercentEscapesUsingEncoding:] to avoid using invalid characters in the URL:

NSString *baseURL = [NSString stringWithFormat:@"http://rate-exchange.appspot.com/currency?from=%@&to=%@&q=%@",strfrom,strto,strgo];
NSURL *url = [NSURL URLWithString:[baseURL stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding]];
NSURLRequest *request = [NSURLRequest requestWithURL:url];
like image 44
trojanfoe Avatar answered Oct 15 '22 09:10

trojanfoe