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....