Welcome to my iphone development code blog

Welcome to my iphone development code blog
"SIMPLICITY IS BEST PROFESSION"

Thursday, July 15, 2010

NET Web Service Classes

Make a class WebServiceHelper.h and put the code below

// WebServiceHelper.h
//

#import


@interface WebServiceHelper : NSObject {
NSString *XMLNameSpace;
NSString *XMLURLAddress;
NSString *MethodName;
NSString *SOAPActionURL;
NSMutableDictionary *MethodParameters;
NSMutableString *MethodParametersAsString;
}
@property(nonatomic, retain) NSString *XMLNameSpace;
@property(nonatomic, retain) NSString *XMLURLAddress;
@property(nonatomic, retain) NSString *MethodName;
@property(nonatomic, retain) NSMutableDictionary *MethodParameters;
@property(nonatomic, retain) NSString *SOAPActionURL;
@property(nonatomic, retain) NSMutableString *MethodParametersAsString;

- (NSMutableData*)initiateConnection;
+ (BOOL)allowsAnyHTTPSCertificateForHost:(NSString*)host;
+ (void)setAllowsAnyHTTPSCertificate:(BOOL)allow forHost:(NSString*)host;
@end

Now in its .m file put the code below

// WebServiceHelper.m
//

#import "WebServiceHelper.h"
#import "XmlDataSetParser.h"

@implementation WebServiceHelper
@synthesize MethodName;
@synthesize MethodParameters;
@synthesize XMLNameSpace;
@synthesize XMLURLAddress;
@synthesize SOAPActionURL;
@synthesize MethodParametersAsString;
- (NSMutableData*)initiateConnection
{


NSMutableString *sRequest = [[NSMutableString alloc] init];
self.SOAPActionURL = [NSString stringWithFormat:@"%@%@",self.XMLNameSpace, self.MethodName];
[sRequest appendString:@""];
[sRequest appendString:@""];
[sRequest appendString:@""];
[sRequest appendString:[NSString stringWithFormat:@"<%@ xmlns=\"%@\">",MethodName, XMLNameSpace]];
if(MethodParametersAsString != nil) [sRequest appendString:MethodParametersAsString];

NSEnumerator *tableIterator = [MethodParameters keyEnumerator];
NSString *keyID;
while(keyID = [tableIterator nextObject])
{
[sRequest appendString:[NSString stringWithFormat:@"<%@>%@", keyID, [MethodParameters objectForKey:keyID], keyID]];
}

[sRequest appendString:[NSString stringWithFormat:@"", MethodName]];
[sRequest appendString:@"
"];
[sRequest appendString:@"
"];

NSURL *myWebserverURL = [NSURL URLWithString:XMLURLAddress];

//NSLog(@"%@",sRequest);

NSMutableURLRequest *request = [NSMutableURLRequest requestWithURL:myWebserverURL];
[request addValue:[NSString stringWithFormat:@"%d",sRequest.length] forHTTPHeaderField:@"Content-length"];
[request addValue:@"text/xml; charset=utf-8" forHTTPHeaderField:@"Content-Type"];
[request addValue:@"beta.ecoscrm.com" forHTTPHeaderField:@"Host"];
[request addValue:SOAPActionURL forHTTPHeaderField:@"SOAPAction"];
[request setHTTPMethod:@"POST"];
[request setHTTPBody:[sRequest dataUsingEncoding:NSUTF8StringEncoding]];
NSURLConnection *conn = [[NSURLConnection alloc] initWithRequest:request delegate:self];
NSMutableData *myMutableData;

if(conn)
{
myMutableData=[[NSMutableData data] retain];
}
else {
NSLog(@"CONNECTION FAILED");

}
[NSURLConnection connectionWithRequest:request delegate:self];

NSError *WSerror;
NSURLResponse *WSresponse;

myMutableData = [[NSMutableData alloc] initWithData:[NSURLConnection sendSynchronousRequest:request returningResponse:&WSresponse error:&WSerror]];

NSString *theXml = [[NSString alloc]initWithBytes:[myMutableData mutableBytes] length:[myMutableData length] encoding:NSUTF8StringEncoding];

NSLog(@"%@",theXml);
[sRequest release];

return myMutableData;
[myMutableData release];
}
+ (BOOL)allowsAnyHTTPSCertificateForHost:(NSString*)host
{
return YES;
}
+ (void)setAllowsAnyHTTPSCertificate:(BOOL)allow forHost:(NSString*)host
{

}
@end

The class is used to call net web service which will return xml schema

you need to make two more classes now

make a class DataSet.h

// DataSet.h


#import


@interface DataSet : NSObject {
NSString *DataSetName;
NSMutableDictionary *Tables;

}
@property (nonatomic, retain) NSMutableDictionary *Tables;
@property (nonatomic, retain) NSString *DataSetName;
-(id)initWithXMLData:(NSMutableData *)data;
-(NSString *)getFirstForTableAndColumn:(NSString *)tableName col:(NSString *)colName;
-(NSMutableDictionary *)getRowsForTableAndColumn:(NSString *)tableName col:(NSString *)colName;
-(NSMutableDictionary *)getRowsForTableAndColumnWhereEqualsString:(NSString *)tableName col:(NSString *)colName where:(NSString *)wherevalue;
-(NSMutableDictionary *)getRowsForTableAndColumnWhereColumnEqualsString:(NSString *)tableName col:(NSString *)colName whereColumn:(NSString *)wherecolumn whereValue:(NSString *)wherevalue;
-(NSMutableDictionary *)getRowsForTableAndColumnWhereEqualsStrings:(NSString *)tableName col:(NSString *)colName where:(NSArray *)wherevalues;
-(void) dealloc;
@end

now put the code below in its .m file

// DataSet.m
//

#import "DataSet.h"
#import "XMLDataSetParser.h"

@implementation DataSet
@synthesize Tables;
@synthesize DataSetName;

-(id)initWithXMLData:(NSMutableData *)data
{

NSXMLParser *parser = [[NSXMLParser alloc] initWithData:data];

XmlDataSetParser *xmParser = [[XmlDataSetParser alloc] init];

[parser setDelegate:xmParser];

[parser setShouldProcessNamespaces:NO];
[parser setShouldReportNamespacePrefixes:NO];
[parser setShouldResolveExternalEntities:NO];

[parser parse];


NSString *theXml = [[NSString alloc]initWithBytes:[data mutableBytes] length:[data length] encoding:NSUTF8StringEncoding];
// NSLog([[NSString alloc] initWithFormat:@"response:%@", theXml, nil]);

self.Tables = [[xmParser thisDataset] Tables];
[parser release];
[xmParser release];

return self;
}

-(NSMutableDictionary *)getRowsForTableAndColumn:(NSString *)tableName col:(NSString *)colName
{
NSMutableDictionary *returnDictionary = [[NSMutableDictionary alloc] init];
NSEnumerator *tableIterator = [self.Tables keyEnumerator];
NSString *tableKey;


while(tableKey = [tableIterator nextObject])
{

if([tableKey isEqualToString:tableName])
{

NSMutableArray *sortedKeys = [NSMutableArray arrayWithArray: [[self.Tables objectForKey:tableKey] allKeys]];
[sortedKeys sortUsingSelector:@selector(compare:)];


NSEnumerator *rowIterator = [sortedKeys objectEnumerator];
NSString *rowKey;


while(rowKey = [rowIterator nextObject])
{

if([rowKey rangeOfString:colName].location != NSNotFound)
{
NSArray *split = [rowKey componentsSeparatedByString:@"--"];

if([[split objectAtIndex:0] isEqualToString:colName])
{
[returnDictionary setObject:[[self.Tables objectForKey:tableKey] objectForKey:rowKey] forKey:[split objectAtIndex:1]];

}
}
}
}

}
if ([returnDictionary count] > 0)
{

return returnDictionary;
}
else
{
return nil;
}

[returnDictionary release];
}

-(NSString *)getFirstForTableAndColumn:(NSString *)tableName col:(NSString *)colName
{
NSMutableDictionary *returnDictionary = [[NSMutableDictionary alloc] init];
NSEnumerator *tableIterator = [self.Tables keyEnumerator];
NSString *tableKey;


while(tableKey = [tableIterator nextObject])
{

if([tableKey isEqualToString:tableName])
{

NSMutableArray *sortedKeys = [NSMutableArray arrayWithArray: [[self.Tables objectForKey:tableKey] allKeys]];
[sortedKeys sortUsingSelector:@selector(compare:)];


NSEnumerator *rowIterator = [sortedKeys objectEnumerator];
NSString *rowKey;


while(rowKey = [rowIterator nextObject])
{

if([rowKey rangeOfString:colName].location != NSNotFound)
{
NSArray *split = [rowKey componentsSeparatedByString:@"--"];

if([[split objectAtIndex:0] isEqualToString:colName])
{
[returnDictionary setObject:[[self.Tables objectForKey:tableKey] objectForKey:rowKey] forKey:[split objectAtIndex:1]];

}
}
}
}

}
if ([returnDictionary count] > 0)
{
return [returnDictionary objectForKey:@"1"];
}
else
{
return nil;
}

[returnDictionary release];
}

-(NSMutableDictionary *)getRowsForTableAndColumnWhereEqualsStrings:(NSString *)tableName col:(NSString *)colName where:(NSArray *)wherevalues
{

return nil;
}

-(NSMutableDictionary *)getRowsForTableAndColumnWhereEqualsString:(NSString *)tableName col:(NSString *)colName where:(NSString *)wherevalue
{
NSMutableDictionary *returnDictionary = [[NSMutableDictionary alloc] init];
NSEnumerator *tableIterator = [self.Tables keyEnumerator];
NSString *tableKey;


while(tableKey = [tableIterator nextObject])
{

if([tableKey isEqualToString:tableName])
{

NSMutableArray *sortedKeys = [NSMutableArray arrayWithArray: [[self.Tables objectForKey:tableKey] allKeys]];
[sortedKeys sortUsingSelector:@selector(compare:)];


NSEnumerator *rowIterator = [sortedKeys objectEnumerator];
NSString *rowKey;

while(rowKey = [rowIterator nextObject])
{

if([rowKey rangeOfString:colName].location != NSNotFound)
{

NSArray *split = [rowKey componentsSeparatedByString:@"--"];
NSString *rowData = [[self.Tables objectForKey:tableKey] objectForKey:rowKey];
if ([rowData isEqualToString:wherevalue])
{
[returnDictionary setObject:rowData forKey:[split objectAtIndex:1]];
}

}
}
}
}

if ([returnDictionary count] > 0)
{

return returnDictionary;
}
else
{
return nil;
}

[returnDictionary release];
}
-(NSMutableDictionary *)getRowsForTableAndColumnWhereColumnEqualsString:(NSString *)tableName col:(NSString *)colName whereColumn:(NSString *)wherecolumn whereValue:(NSString *)wherevalue
{
NSMutableDictionary *returnDictionary = [[NSMutableDictionary alloc] init];
NSEnumerator *tableIterator = [self.Tables keyEnumerator];
NSString *tableKey;


while(tableKey = [tableIterator nextObject])
{


if([tableKey isEqualToString:tableName])
{

NSMutableArray *sortedKeys = [NSMutableArray arrayWithArray: [[self.Tables objectForKey:tableKey] allKeys]];

[sortedKeys sortUsingSelector:@selector(compare:)];

NSEnumerator *rowIteratorWhereColumn = [sortedKeys objectEnumerator];
NSString *rowWhereKey;
while(rowWhereKey = [rowIteratorWhereColumn nextObject])
{

if([rowWhereKey rangeOfString:wherecolumn].location != NSNotFound)
{

NSArray *split = [rowWhereKey componentsSeparatedByString:@"--"];

NSString *coldata = [[self.Tables objectForKey:tableKey] objectForKey:rowWhereKey];

if([coldata isEqualToString:wherevalue])
{
if( [[self.Tables objectForKey:tableKey] objectForKey:[NSString stringWithFormat:@"%@--%@", colName, [split objectAtIndex:1]]]==nil)
{
}
else
{
[returnDictionary setObject:[[self.Tables objectForKey:tableKey] objectForKey:[NSString stringWithFormat:@"%@--%@", colName, [split objectAtIndex:1]]] forKey:[split objectAtIndex:1]];
}
}
}
}
}
}

if ([returnDictionary count] > 0)
{

return returnDictionary;
}
else
{
return nil;
}

[returnDictionary release];
}

-(void)dealloc
{
self.Tables = nil;
[super dealloc];

}

@end

this will create dataset

u need one more class to parse ur xml

make class

// XMLDataSetParser.h
//


#import
#import "DataSet.h"

@interface XmlDataSetParser : NSObject {
DataSet *thisDataset;
Boolean beginDataset;
Boolean beginTable;
NSString *currentTable;
NSString *currentColumn;
}

@property (nonatomic, retain) DataSet *thisDataset;
@property (nonatomic, retain) NSString *currentTable;
@property (nonatomic, retain) NSString *currentColumn;



- (void)parserDidStartDocument:(NSXMLParser *)parser;
- (void)parserDidEndDocument:(NSXMLParser *)parser;
- (void)parser:(NSXMLParser *)parser didStartElement:(NSString *)elementName namespaceURI:(NSString *)namespaceURI qualifiedName:(NSString *)qName attributes:(NSDictionary *)attributeDict;
- (void)parser:(NSXMLParser *)parser didEndElement:(NSString *)elementName namespaceURI:(NSString *)namespaceURI qualifiedName:(NSString *)qName;
- (void)parser:(NSXMLParser *)parser foundCharacters:(NSString *)string;

@end

put the code below in its m file

// XMLDataSetParser.m
//


#import "XmlDataSetParser.h"
#import "DataSet.h"

@implementation XmlDataSetParser
@synthesize thisDataset;
@synthesize currentTable;
@synthesize currentColumn;

#pragma XML Parser Delegate Methods
- (void)parserDidStartDocument:(NSXMLParser *)parser
{
thisDataset = [[DataSet alloc] init];
}
- (void)parserDidEndDocument:(NSXMLParser *)parser
{
}


- (void)parser:(NSXMLParser *)parser didStartElement:(NSString *)elementName namespaceURI:(NSString *)namespaceURI qualifiedName:(NSString *)qName attributes:(NSDictionary *)attributeDict
{
if (beginDataset == YES)
{

thisDataset.DataSetName = elementName;
thisDataset.Tables =[[NSMutableDictionary alloc] init];
beginDataset = NO;
}
else
{
if ([elementName isEqualToString:@"diffgr:diffgram"]) {

beginDataset = YES;
}

if (thisDataset.DataSetName != nil && beginTable == YES)
{

if([currentColumn isEqualToString:@""] || currentColumn == nil)
{
currentColumn = elementName;
}
}
if (thisDataset.DataSetName != nil && beginTable == NO)
{

if([thisDataset.Tables objectForKey:elementName] == nil)
{
[thisDataset.Tables setObject: [[NSMutableDictionary alloc] init] forKey:elementName];
currentTable = elementName;

}
beginTable = YES;
}

}


}

- (void)parser:(NSXMLParser *)parser didEndElement:(NSString *)elementName namespaceURI:(NSString *)namespaceURI qualifiedName:(NSString *)qName
{
if(beginTable == YES && [elementName isEqualToString:currentTable])
{
beginTable = NO;
}

if(beginTable == YES && currentColumn != @"")
{
currentColumn = @"";
}

}


- (void)parser:(NSXMLParser *)parser parseErrorOccurred:(NSError *)parseError {
NSString * errorString = [NSString stringWithFormat:@"Unable to convert XML into dataset: %@", [parseError description]];
NSLog(@"error parsing XML: %@", errorString);

UIAlertView * errorAlert = [[UIAlertView alloc] initWithTitle:@"Error loading content" message:errorString delegate:self cancelButtonTitle:@"OK" otherButtonTitles:nil];
[errorAlert show];
}

- (void)parser:(NSXMLParser *)parser foundCharacters:(NSString *)string
{

NSMutableDictionary *table = [[NSMutableDictionary alloc] initWithDictionary:[thisDataset.Tables objectForKey:currentTable]];

NSEnumerator *myIterator = [table keyEnumerator];
NSString *aKey;
NSInteger intCnt = 0;

while(aKey = [myIterator nextObject])
{

NSArray *split = [[NSArray alloc] initWithArray:[aKey componentsSeparatedByString:@"--"]];

if([[split objectAtIndex:0] isEqualToString:currentColumn])
{
intCnt++;
}
[split release];
}

NSString *colName = [[NSString alloc] initWithFormat:@"%@--%d", self.currentColumn, (intCnt+1), nil];
[table setObject:string forKey:colName];
[thisDataset.Tables setObject:table forKey:currentTable];
[colName release];
[table release];
}

@end

now u have all the classes defined for web service

now here below i show you how to use it

#import "DataSet.h"
#import "WebServiceHelper.h"

import the above in class in which u wanna call web service
now use below code

WebServiceHelper *DataCon = [[WebServiceHelper alloc] init];


DataCon.XMLNameSpace = @"http://tempuri.org/";
DataCon.XMLURLAddress = @"http://yourdomain.com/webservicesfolder/service1.asmx";
DataCon.MethodName = @"MyMethodName";
DataCon.MethodParameters = [[NSMutableDictionary alloc] init];
[DataCon.MethodParameters setObject:[[NSUserDefaults standardUserDefaults] stringForKey:@"name_preference"] forKey:@"UserName"];
[DataCon.MethodParameters setObject:[[NSUserDefaults standardUserDefaults] stringForKey:@"password_preference"] forKey:@"Password"];
[DataCon.MethodParameters setObject:search forKey:@"GroupId"];
tdata = [DataCon initiateConnection];

DataSet *dsMyDataSet = [[DataSet alloc] initWithXMLData:tdata];

NSMutableArray *Name = [[NSMutableDictionary alloc] init];
NSMutableArray *ID = [[NSMutableDictionary alloc] init];
NSMutableArray *SGN = [[NSMutableDictionary alloc]init];

ID = [dsMyDataSet getRowsForTableAndColumn:@"_x0030_" col:@"Id"];
Name = [dsMyDataSet getRowsForTableAndColumn:@"_x0030_" col:@"DisplayName"];
SGN = [dsMyDataSet getRowsForTableAndColumn:@"_x0030_" col:@"SubGroupName"];

Enjoyy !

No comments:

Post a Comment