Thursday, December 22, 2011

iPhone with WebService

Here, we can find how to connect a web-service in iPhone application to view list.
I assume that a web service is already created in or Jsp or PhP which return data in json format. Now, we have three things to do, to connect the web service. 1> Connect web service; 2> Access data from it and 3> Show it in a list.
First click on xCode icon. It will open the xCode tool.

Now, select option to create new project. Choose view base application from the list. 

Give a name for the project and we are ready to go now.

There will be few files in project. Some of them are not require any changes. Delete -  ViewController.h and ViewController.m  and then add file from File->New File .

Then Select UIViewController type and do next.

Select UITableViewController and give name iWebServiceViewController and save.  The files with which we will work are-  iWebServiceViewController.h and iWebServiceViewController.m  from Classes folder .  ‘.H’ -file contains a kind of signature of ‘.M’ file.  Another one is - iWebServiceViewController.xib in Resources folder under project. It will be used for design the application window.

1>Connect web service[ using Object-C]
In  iWebServiceViewController.h ->
#import <UIKit/UIKit.h>
#define wsURL @"http://yourwebserviceurl"
@interface listEmpViewController : UITableViewController {
NSMutableArray *jsonResult;


In  iWebServiceViewController.m ->
//======User Defined==============
NSURL *url=[NSURL URLWithString:wsURL];
NSData *data=[NSData dataWithContentsOfURL:url];
[self getData:data];
In viewDidLoad method->
- (void)viewDidLoad {
            [super viewDidLoad];
[self connectService];

2> Access data from it->
-(void)getData:(NSData *) data{
            NSError *error;
            jsonResult = [NSJSONSerialization JSONObjectWithData:data options:kNilOptions error:&error];

3> Show it in a list ->

In  iWebServiceViewController.m ->

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    // Return the number of rows in the section.
    return [jsonResult count];

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    static NSString *CellIdentifier = @"Cell";
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
    if (cell == nil) {
        cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier] autorelease];
NSDictionary *info= [json objectAtIndex:indexPath.row];
cell.textLabel.text=[info objectForKey:@"forename"];
    return cell;

At last-
In AppDelegate.m changes need to be done in import->
#import “iWebServiceViewController.h”
And also in didFinishLaunchingWithOptions->
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
    // Override point for customization after application launch.
iWebServiceViewController *listEmp=[[ iWebServiceViewController alloc] init];
listEmp.title=@"Emp table";
self.window.rootViewController= listEmp;
// Set the view controller as the window's root view controller and display.
            //self.window.rootViewController = self.viewController;
            [self.window makeKeyAndVisible];
            return YES;

That’s it, Enjoy… J

No comments: