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-09-2008, 09:45 PM   #12 (permalink)
varchar
Registered Member
 
Join Date: Aug 2008
Posts: 95
Default

See below for all the code:

WebServiceHelper.h

Code:
#import <UIKit/UIKit.h>


@interface WebServiceHelper : NSObject {

	
}

+(NSURLRequest *) generateWebServiceURLRequest : (NSString *) url: (NSString *) methodName: (NSArray *) keys: (NSArray *) objects;
+(NSURL *) generateWebServiceHTTPGetURL : (NSString *) url: (NSString *) methodName: (NSArray *) keys: (NSArray *) objects;

@end

WebServiceHelper.m

Code:
#import "WebServiceHelper.h"


@implementation WebServiceHelper



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

@end
MyViewController.h (obviously any view controller)

Code:

#import <UIKit/UIKit.h>


@interface MyViewController : UIViewController {
	
	IBOutlet UILabel *lblUserName;
	IBOutlet UILabel *lblPW;

}


@property (nonatomic, retain) UILabel *lblUserName;
@property (nonatomic, retain) UILabel *lblPW;


-(IBAction) callMyWebService: (id) sender;

@end
MyViewController.m

Code:

#import "myViewController.h"

@implementation myViewController

@synthesize lblUserName, lblPW;

-(IBAction) callMyWebService: (id) sender {


NSURL *theURL;
	

	NSArray *keys = [NSArray arrayWithObjects:@"userName",@"passWord", nil];
	NSArray *keyvalues = [NSArray arrayWithObjects:lblUserName.text,lblPW.text, nil];	
	

	
	theURL = [WebServiceHelper generateWebServiceHTTPGetURL: @"http://www.mywebsite.com/mywebservice.asmx": @"mywebservicemethod": keys: keyvalues];
	

// The rest of the code you will have to use NSXMLParser to parse the WS XML

}


I hope the above will help you... maybe some syntax errors... wrote it by hand....

Good luck....
varchar is offline   Reply With Quote
 

» Advertisements
» Online Users: 339
17 members and 322 guests
ADY, BrianSlick, dacapo, fiftysixty, john love, Joseph Nardone, laurielaptop, ligerligerliger, mizzytheboy, oztemel, poisenden, Sunny46, syver, themathminister, TheWebWizz, XRumerTest
Most users ever online was 1,187, 10-11-2011 at 08:09 AM.
» Stats
Members: 158,876
Threads: 89,225
Posts: 380,701
Top Poster: BrianSlick (7,129)
Welcome to our newest member, jorge599
Powered by vBadvanced CMPS v3.1.0

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