René van Mil

SAP Gateway on iOS With AFNetworking

SAP Gateway is an ABAP framework part of SAP NetWeaver which allows you to create RESTful OData webservices on the SAP system. Data can be transmitted in either JSON or XML format. It’s a great solution to integrate SAP as a backend system into mobile or web applications (as opposed to the complexity and rigidity of PI and SOAP services).

To demonstrate how to use SAP Gateway to integrate SAP into an iOS 7 app, I have created a simple webservice for the well-known SFLIGHT demo data (make sure to run the SAPBC_DATA_GENERATOR program to generate some demo data first) in SAP, and an iOS 7 app which displays the data.

Gateway Webservice

SAP Gateway uses so-called abstract ‘data provider’ and ‘model provider’ classes, inheriting from a bunch of other classes in the framework, which need to be implemented by you. The good news is a lot of this stuff will be generated once and implementing the custom parts is easy.

To create the model for this demo, start transaction SEGW and create a new project named ZSFLIGHTGW. Open the project and follow these steps:

1. Create an Entity Type named Flights and enter SFLIGHT as the ABAP structure name

2. Add the following properties:

1
2
3
4
5
6
Name      Key  Edm Core Type  ABAP Field
CARRID    X    Edm.String     CARRID
CONNID    X    Edm.String     CONNID
FLDATE    X    Edm.DateTime   FLDATE
PRICE          Edm.Decimal    PRICE
CURRENCY       Edm.String     CURRENCY

3. Create an Entity Set named FlightSet

4. Click the ‘Generate Runtime Objects’ button (the red-white circle) and accept the defaults

5. After generation, you should be able to see the generated service implementation and its operations

In this demo we will only read a list of SFLIGHT records, so the only operation which needs to be implemented is the GetEntitySet (Query) operation. As you can see on the previous screenshot, the operation can be implemented with class ZCL_ZSFLIGHTGW_DPC_EXT in the FLIGHTSET_GET_ENTITYSET method.

6. Implement the FLIGHTSET_GET_ENTITYSET method:

FLIGHTSET_GET_ENTITYSET method
1
2
3
method flightset_get_entityset.
  select * into table et_entityset up to 1000 rows from sflight.
endmethod.

7. Create the webservice (with SAP customizing transaction SPRO) as shown on these screenshots:

The webservice is now ready and you should be able to reach it at https://yoursapserver.local:<port>/sap/opu/odata/sap/zsflightgw_srv?sap-client=<client> (replace <port> and <client> with proper numbers for your system). Performing a GET request on that url (make sure you provide the correct credentials through HTTP basic authentication) should return something like this:

Adding the parameter $format=json will have the server return a response in JSON format. So if you prefer to work with JSON instead of XML, you should always add this parameter to every request.

iOS App

The app will show a list of flights and when tapping one of the flights it will display the details of that flight.

To perform the HTTP requests to SAP Gateway I use the AFNetworking framework. It makes performing HTTP requests a lot easier than the standard iOS SDK, and it powers some of the most popular and critically-acclaimed apps on the iPhone, iPad, and Mac.

The SAPGatewayClient class provides the following method to retrieve all flights using SAP Gateway:

List the flights using AFNetworking
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
- (void)listFlightsWithSuccess:(void (^)(NSMutableArray *flights))success
                       failure:(void (^)(NSError *error))failure;
{
    /*
     The URL to SAP Gateway, which we'll send the GET request to. We want to list the entire FlightSet.
     Make sure to add the '$format=json' parameter, so the response will be formatted in JSON
     */
    NSString *urlString = [NSString stringWithFormat:@"%@FlightSet?$format=json", baseUrl];
    /*
     Perform the GET request.
     If the request succeeds, the list of flights is read from the response. SAP Gateway always returns a single JSON object, which always contains a single property called 'd', which always contains a single array called 'results'.
     For this call the 'results' array contains all the flights.
     After reading the flights array, the success callback is executed providing the retrieved array of flights.
     */
    [[self manager] GET:urlString parameters:nil success:^(AFHTTPRequestOperation *operation, id responseObject) {
        NSMutableArray *flights = [[(NSMutableDictionary *)responseObject valueForKey:@"d"] valueForKey:@"results"];
        if (success) {
            success(flights);
        }
    } failure:^(AFHTTPRequestOperation *operation, NSError *error) {
        NSLog(@"%@", error);
        if (failure) {
            failure(error);
        }
    }];
}

And that is all you need to integrate SAP into your iOS app using SAP Gateway. Everything else is just common viewcontroller code and a storyboard. You can find the complete iOS project and source code on GitHub: SAP Gateway App Demo.

Comments

Real Time Web Analytics