Skip to content

1.2 Create an iOS client app in Xcode

Lewis Cheng edited this page Aug 1, 2014 · 5 revisions

Generate client code using codegen tool:

./odata-codegen-tool "http://services.odata.org/V4/(S(2ldtxpshdgzdinhcyq5mfstr))/TripPinServiceRW" trippin_service

In project, add a User Defined Setting ODTACPP_DIR which points to the location of odatacpp code. In Header Search Paths, add following include path:

$(ODTACPP_DIR)/include
$(ODTACPP_DIR)/lib/casablanca/Release/include
$(ODTACPP_DIR)/ios/boost.framework/Headers
/usr/include/libxml2

Link Binary with Libraries, add following libraries:

$(ODTACPP_DIR)/lib/casablanca/Build_iOS/build.ios/libcpprest.a
$(ODTACPP_DIR)/ios/boost.framework (add /Users/xubin/Code/odatacpp/ios/boost.framework/Versions/A to Library Search Paths)
$(ODTACPP_DIR)/lib/casablanca/Build_iOS/openssl/lib/libcrypto.a
$(ODTACPP_DIR)/lib/casablanca/Build_iOS/openssl/lib/libssl.a
$(ODTACPP_DIR)/ios/build.ios/libodata-library.a
$(ODTACPP_DIR)/ios/build.ios/libodata-client.a
libxml2.dylib

Change MasterViewController.m to MasterViewController.mm

Add trippin_service.h and trippin_service.cpp to project. Open MasterViewController.m, add odata_client.h and trippin_service to include list:

#include "odata/client/odata_client.h"
#include "trippin_service.h"

using namespace Microsoft_OData_SampleService_Models_TripPin;

In MasterViewController, add a variable of type std::vector<std::share_ptr<AirLine>> named airlines to store the airline objects.

@interface MasterViewController () {
    NSMutableArray *_objects;
    std::vector<std::shared_ptr<Airline>> airlines;
}
@end

Add method loadInitialData to load the airlines from server:

- (void) loadInitialData
{
    ::utility::string_t service_root(U("http://services.odata.org/V4/(S(2ldtxpshdgzdinhcyq5mfstr))/TripPinServiceRW/"));
    
    auto service_context = std::make_shared<DefaultContainer>(service_root);
   
    airlines = service_context->create_airlines_query()->execute_query().get();
}

Call loadInitialData in viewDidLoad method:

[self loadInitialData];

Update numberOfRowsInSection

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return airlines.size();
}

Run the app, you should see the table with three lines.

Update method cellForRowAtIndexPath:

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"Cell" forIndexPath:indexPath];

    auto airline = airlines[indexPath.row];
    auto name = airline->get_name();
    cell.textLabel.text = [NSString stringWithCString:name.c_str() encoding:NSASCIIStringEncoding];
    
    return cell;
}

airlines