Advertise Mobile SDKs Books Events Forum News Social Networking Support Us
Follow @iphonedevsdk on Twitter

Mockup & CodeGen, iPhone & iPad
($9.99)

Make your own iPhone apps
and run them live!
(free)

Manu
($0.99)

Want your application or service advertised on iPhone Dev SDK?

Go Back   iPhone Dev SDK Forum

View Single Post
Old 09-01-2008, 09:56 PM   #8 (permalink)
varchar
Registered Member
 
Join Date: Aug 2008
Posts: 95
Default Generic Call WS

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;
}

Last edited by varchar; 09-09-2008 at 05:51 PM.
varchar is offline   Reply With Quote
 

» Advertisements
» Online Users: 745
13 members and 732 guests
Bertrand21, betterlee, chiataytuday, gascertificate, kampftrinker, Macaret, meribran, nidie, soonw29, superblooddrainer, wilsonn44, wodanbrlam
Most users ever online was 1,187, 10-11-2011 at 08:09 AM.
» Stats
Members: 158,827
Threads: 89,207
Posts: 380,638
Top Poster: BrianSlick (7,129)
Welcome to our newest member, meribran
Powered by vBadvanced CMPS v3.1.0

All times are GMT -5. The time now is 02:52 AM.
Powered by vBulletin® Version 3.8.0
Copyright ©2000 - 2012, Jelsoft Enterprises Ltd.