SAP XSOdata / Gateway service consumption in XCode Swift
Happy Friday 🙂
I have started learning XCode Swift programming and would like to share my learning experience with all developers.
As we all know, SAP has already provided SAP Cloud IOS SDK (https://developer.apple.com/sap/)) to connect with SAP Cloud and XCode.
I have tried the mentioned way to consume OData (https://services.odata.org/V3/OData/OData.svc/) service following Set Up the SAP Cloud Platform SDK for iOS tutorial:
However, if we don’t want to go with SAP Cloud Platform SDK for iOS then what??
I have developed an alternative option of customized application using SAP gateway service.
Below is a small application, which I have developed using all CURD service without using SAP Cloud Platform SDK for iOS.
A basic XCode and swift program knowledge is required to develop this application.
Following are the steps, to developed an application in XCode using Swift and other libraries.
Create a simple DB table in trial cloud account:
Table Name : USER_MASTER
Create XSOData service in cloud editor with all CURD access.
Install CocoaPods on MAC system using
CocoaPods is required to add external library in XCode project and enable required functionality.
Create a new XCode project
Once project field gets ready, close the project. Create a profile pod file using “POD INIT” command line. Open profile file using POD application and add the below line and install to enable respective libraries into project files.
pod `Alamofire’, ‘~> 4.4’
pod ‘SwiftyJSON’,’~> 3.1′
pod ‘ODataSwift’,’~> 1.0′
After installation, a new workspace extension file will be created.
Note: To get the pods functionality always use workspace extension file for the project to make further changes.
Below are the screens details, which has been created for this example:
Pod Alamofire provides functionalities to perform CURD process for rest API. The same has been used in this example to fetch, create, delete and modify the existing records.
The main challenge was to work with credentials to do all CURD process.
AlamoFire.request function to use all above process with required parameters.
|URL||User URL information of XSOData service including entity name|
|Method||. connect||check the connectivity|
|. delete||delete a respective record|
|. put||update a record based on keys|
|. get||get all records based on URL|
|. post||create a record|
|Parameters||Parameters information contains the field and value information of using OData service only for. PUT and. POST method|
|Encoding||Encoding information of using OData service||JSONEncoding.default,|
|HTTPHeaders||This contains an array of HTTP call.|
Below are the key and respective values, which need to be maintained as array for HTTPHeader parameter:
Below are the key and value in array format, which requires when we are trying CRUD functionality with OData service which needs user credential.
“Authorization”: “Basic \(UserCredentail)”,
Below are the key-values, which get the formatted details of using OData service.
“Content-Type”: “application/json; charset=utf-8”,
Using above functionally, I have developed a basic application of user details to create new user, delete or update existing user and see all saved user.
Below is the code part using Alamofire.request function:
Fetch all User information
Create a new user, in below code maintaining parameters value as new user details with all field name.
Delete user: Creating URL with selected user id.
Update user: forming parameters with non-primary fields and passing URL with selected user id.
The Application login screen:
In this screen, I have used a functionality to store entered user id and password into application level. So, every time same user does not need to enter credential.
User details screen:
This screen is contains table control using labels to display user name, mail and mobile number inside table cell.
Add a new user:
This screen will be displaying, when “ADD USER” option select. In this screen extension and mobile number has validation to display an error message if extension length exceeds more than 4 or character exceeds 10 respectively.
On “Add user” selection, a new record will be added into data based and the same will be updated into application level table control.
To update an existing user: added these options in table cell level.
On selection of “Update” option, then same add user screen will be displaying to selected user data for further modification.
Once update, the value will be captured in data base table and application table control.
The same way, on selection of delete option or swipe the table cell to left side of table. The selected record will be deleting from data base table and application table control.
Below is the directory structure and files information, which I have created for this project.
To consume non-secure site, (local server) below keys and dictionary information should be maintained in info.plist file inside project directory as below:
Or manually modify the file with below below keys / disctionary values :
Project files are available in github.
Happy learning 🙂