Cory Rylan

My name is , Google Developer Expert, Speaker, Software Developer. Building Design Systems and Web Components.

Follow @coryrylan
Cory Rylan Blog

IOS Web Service

Cory Rylan

- 1 minute

This following example is a small snippet of code I found to call a simple web service and return it to a NSURL string. You will need to use a XML parser to parse your web services data. This web service sends the request information in the url.

-(BOOL)CallService:(NSString *)value
{
// Call web service to verify a session object or string
BOOL result = YES;

// Create the objects for calling the web service
NSString *buildURL = [NSString stringWithFormat:@"https://coryrylan.com/web-services/MyWebServices.asmx/myServiceCheck?IDReceived=%@", ID];
NSURL *url = [NSURL URLWithString:buildURL];

//Call the request
NSURLRequest *urlRequest = [NSURLRequest requestWithURL:url;
cachePolicy:NSURLRequestReturnCacheDataElseLoad
timeoutInterval:1];

// Create the objects for parsing the xml data returned by web service
NSData *data;
NSURLResponse *response;
NSError *error;

// Call the web service
xmlData = [NSURLConnection sendSynchronousRequest:urlRequest returningResponse:&response error:&error];

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

[parser setDelegate:self]; // You can create a parser method to remove any whitespaces or apending chars if needed

reply = [[NSMutableString alloc]init];

// Read the xml data returned by the web service
[parser parse];
if([reply isEqualToString:@"yourErrorResponse"]) //If your service returned ect do this
{
ErrorMessage = @"Error";
return NO;
}

if([reply isEqualToString:@""] || reply == NULL) // Else if no reply do that, or use error object
{
//No reply from web service
ErrorMessage = @"Connection Error";
return NO;
}
return result; //Else return the result or error
}
//XML PARSER
-(NSString)parser:(NSXMLParser *)parser foundCharacters:(NSString *)string
{
reply = [NSString stringWithString:string];
return reply;
}
Twitter Facebook LinkedIn Email
 

No spam. Short occasional updates on Web Development articles, videos, and new courses in your inbox.

Related Posts

Cory Rylan Blog

IOS Character Counter

A short code example of how to make a character counter for IOS in Objective C.

Read Article
Cory Rylan Blog

IOS Journey the Observer

A short code example of how to use the observer object for IOS in Objective C.

Read Article
Cory Rylan Blog

IOS Journey Load Multi-Threading

A short code example of how to use multi-threading for IOS in Objective C.

Read Article