Wednesday, August 14, 2013

Calling .NET REST web service from iOS using AFNetworking. – Part 1


Today I’m going to talk about how to call a .NET REST web service. Actually, when I stared to develop iphone applications, I had to work with .NET REST web services. At that time I had to face lot of difficulties when calling web services. It was very hard to find a better solution by searching the web at that time. So I thought to tell you how your .Net web service should be and how to call it from iOS. In my next post I’m going to talk about how to upload an image from iOS to .NET REST web service.

Here I’m using AFNetworking to call web service. You can download it from here.

In this example I’m calling a GET method in .NET web service, which returns a string as a result. This method accept the user name, and it returns “#VALID#” if the user is in the main database, if not service will return “#INVALID#”.

This is how your service interface class should look like.


using System.ServiceModel;
using System.ServiceModel.Web;
namespace MyDemo
{
[ServiceContract]
public interface IUserService
{
[OperationContract]
[WebInvoke(UriTemplate = "IsUserExists?userName={userName}", BodyStyle = WebMessageBodyStyle.Bare, RequestFormat =
WebMessageFormat.Json, ResponseFormat = WebMessageFormat.Json, Method = "GET")]
string IsUserExists(string userName);
}
}
view raw gistfile1.txt hosted with ❤ by GitHub

This is how your service.svc file should look like.


namespace MyDemo
{
public class UserService : IUserService
{
public string IsUserExists(string userName)
{
try
{
if(isUserExists)
return “#VALID#”.
else
return “#INVALID#”.;
}
catch (Exception exception)
{
throw new WebException(exception.Message);
}
}
}
}
view raw gistfile1.txt hosted with ❤ by GitHub

In the iOS this is how you should call it. Here I have written my method using blocks so after calling the web service it will notify the caller. 

+(void)isUserExists:(NSString *)userName :(void (^)(BOOL result, NSError *error))block
{
NSURL *url = [NSURL URLWithString:[NSString stringWithFormat:@"%@UserService.svc/",yourServiceURL]];
NSDictionary *params = [NSDictionary dictionaryWithObjectsAndKeys:
userName,@"USERNAME",
nil];
AFHTTPClient *client = [[AFHTTPClient alloc] initWithBaseURL:url];
NSMutableURLRequest *request = [client requestWithMethod:@"GET" path:@"IsUserExists" parameters:params];
[request setValue:@"application/x-www-form-urlencoded; charset=UTF8" forHTTPHeaderField:@"Content-Type"];
AFHTTPRequestOperation *operation = [client HTTPRequestOperationWithRequest:request success:^(AFHTTPRequestOperation
*operation, id responseObject)
{
NSString *resultStr = [[NSString alloc] initWithData:responseObject encoding:NSUTF8StringEncoding];
if([resultStr isEqualToString:@"#VALID#"])
{
block(YES,nil);
}
else
{
block(NO,nil);
}
}
failure:^(AFHTTPRequestOperation *operation, NSError *error)
{
block(NO,error);
}];
[client enqueueHTTPRequestOperation:operation];
}
view raw gistfile1.txt hosted with ❤ by GitHub

No comments:

Post a Comment