When you start writing applications for IOS (I-Phone), Communication with a Web server is a very crucial phase. You need to call web-services many times to get and post data from the server, As we all know here we have two methods to call web-services in IOS “GET” and “POST”. Frequently POST method is used to implement the web-services.
So I come up with a solution for calling web-services. Sometimes it becomes very hesitating to call web API in each view controller. According to Apple documentation, NSurlConnection is deprecated after IOS 7. NSurlSession is an alternative class that we can use. NsurlSession is more efficient, flexible than NSurlConnection. NSurlSession has better-caching capability for maintaining global as well as local session caching. You can create a singleton class as well for calling web service and write this method so that a single instance can be created, Need not make multiple objects of that class.
Benefits of using NSurlSession than NSurlConnection:
1) NSurlSession provides background upload and downloads task configuration it makes more flexible to communicate with the web server.
2) NSurlSession supports Global catching.
3) NSurlSession has better session configuration ability than NSurlConnection.
4) NSurlSession Can able to resume and stop communication with the web server.
5) Speed and concurrency of NSurlSession are better than NSurlConnection.
Insert the below function into your .m file.
1 2 3 4 5 6 |
-(NSDictionary *)webParsingMethod:(NSString *)webServiceCompleteUrl :(NSString *)parameter
{
NSDictionary *jsonDictionary;
NSURL *url=[NSURL URLWithString:webServiceCompleteUrl];
NSMutableURLRequest *request=[[NSMutableURLRequest alloc]initWithURL:url];
[request setHTTPMethod:@"POST"];
[request setValue:@"application/x-www-form-urlencoded" forHTTPHeaderField:@"Content-Type"]; [request setHTTPBody:[parameter dataUsingEncoding:NSUTF8StringEncoding]]; NSData *data=[NSURLConnection sendSynchronousRequest:request returningResponse:nil error:nil]; jsonDictionary=[NSJSONSerialization JSONObjectWithData:data options:kNilOptions error:nil]; return jsonDictionary;
} |
And implement the function when you require.
1 2 3 4 |
NSString *webService=@"ws-login"; NSString *completeWebService=[NSString stringWithFormat:@"%@%@",@"www.yourdomain.com",webService]; NSString *parameter=[NSString stringWithFormat:@"email=%@&password=%@",@"sp@sp.com",@"123456"]; NSDictionary *dic=[ webParsingMethod:completeWebService :parameter]; |
You will receive a response in the dictionary. if you have any question please ask. Happy coding. Please comment and share the article if you find something useful.
Thanks for sharing.
Can you write basic layout design for normal app ?
I'm very new to iOS platform.
Thanks.
yes i will try to add new post as early as possible.
Your approach is not right to make a common method for Webservices, this will block your main thread as you are calling synchronous method to get data, instead of synchronous use asynchronous methods.
using synchronous or asynchronous methodology depends on the how you are going to implement this web service,if you are using synchronous methodology fetch the dictionary data inside it.
dispatch_async(dispatch_get_main_queue(),^{
//fetch your contents which comes from web service.
});
Some party use aluminium scaffolding for dinning table. Such as 300 member food taken in 60 Set of aluminium scaffolding table model.