This code is great... helped me out with a core app I am doing.
I hope you do not mind, I used your code and created 2 generic methods:
Code:
/*
url - string url of web service (including .asmx)
methodName - string methodname(operation) of webservice
keys = parameters of web service
objects = values of each parameter in web service
example:
NSArray *keys = [NSArray arrayWithObjects:@"USZip", nil];
NSArray *keyvalues = [NSArray arrayWithObjects:@"11235", nil];
[self generateWebServiceURLRequest: @"http://www.mywebsite.com/mywebservice.asmx" : @"getMyData": keys: keyvalues]
*/
+(NSURLRequest *) generateWebServiceURLRequest : (NSString *) url: (NSString *) methodName: (NSArray *) keys: (NSArray *) objects
{
NSDictionary *params = [NSDictionary dictionaryWithObjects:objects forKeys:keys];
NSString *newMethodName;
// We need to start the methodName with a / - since we are using GET Request method
newMethodName = @"/";
newMethodName = [newMethodName stringByAppendingString: methodName];
url=[url stringByAppendingString:newMethodName];
BOOL firstKey=TRUE;
for (NSString *key in params)
{
NSString *value=[params objectForKey:key];
if (firstKey) url=[url stringByAppendingString:@"?"]; else url=[url stringByAppendingString:@"&"];
url=[url stringByAppendingString:key];
url=[url stringByAppendingString:@"="];
url=[url stringByAppendingString:value];
firstKey=FALSE;
}
NSURLRequest *theRequest=[NSURLRequest requestWithURL:[NSURL URLWithString:url]
cachePolicy:NSURLRequestUseProtocolCachePolicy
timeoutInterval:60.0];
return theRequest;
}
+(NSURL *) generateWebServiceHTTPGetURL : (NSString *) url: (NSString *) methodName: (NSArray *) keys: (NSArray *) objects
{
NSDictionary *params = [NSDictionary dictionaryWithObjects:objects forKeys:keys];
NSString *newMethodName;
NSURL *resultURL;
// We need to start the methodName with a / - since we are using GET Request method
newMethodName = @"/";
newMethodName = [newMethodName stringByAppendingString: methodName];
NSLog(@"generateWebServiceHTTPGetURL: newMethodName: %@",newMethodName);
url=[url stringByAppendingString:newMethodName];
BOOL firstKey=TRUE;
for (NSString *key in params)
{
NSString *value=[params objectForKey:key];
if (firstKey) url=[url stringByAppendingString:@"?"]; else url=[url stringByAppendingString:@"&"];
url=[url stringByAppendingString:key];
url=[url stringByAppendingString:@"="];
url=[url stringByAppendingString:value];
firstKey=FALSE;
}
NSLog(@"generateWebServiceHTTPGetURL: URL: %@",(NSString *)url);
resultURL = [NSURL URLWithString:url];
return resultURL;
}