Personal Insights
SAP XSOdata / Gateway service consumption in XCode Swift
Hi All,
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:
https://developers.sap.com/group.ios-sdk-setup.html
https://developers.sap.com/mission.sdk-ios-one-code-line.html?url_id=text-us-recommendation
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
Details :
Create XSOData service in cloud editor with all CURD access.
Install CocoaPods on MAC system using
https://wiki.base22.com/btg/how-to-setup-cocoapods-65373274.html
https://guides.cocoapods.org/using/getting-started.html
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.
Parameters | Value | Information |
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)”,
“X-CSRF-Token”: “Fetch”,
Below are the key-values, which get the formatted details of using OData service.
“Content-Type”: “application/json; charset=utf-8”,
“Accept”: “application/json”
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 🙂
Praveer
Hi Praveer, I would like to take a look to your project and test it in my environment.
Unfortunately, after installing CocoaPods and cloning the Workspace from GitHub, I receive the following errors:
AlamofireUser/AlamofireUser.xcodeproj The file “Pods-AlamofireUser.release.xcconfig” couldn’t be opened because there is no such file. (AlamofireUser/Pods/Target Support Files/Pods-AlamofireUser/Pods-AlamofireUser.release.xcconfig)
AlamofireUser/Pods/Pods.xcodeproj The file “SwiftyJSON.xcconfig” couldn’t be opened because there is no such file. (AlamofireUser/Pods/Target Support Files/SwiftyJSON/SwiftyJSON.xcconfig)
missing module map file: 'AlamofireUser/Pods/Target Support Files/SwiftyJSON/SwiftyJSON.modulemap
missing module map file: 'AlamofireUser/Pods/Target Support Files/SwiftyJSON/SwiftyJSON.modulemap
Can you help me on how to get rid of those?
Sincerely,
Luca
Hi Luca,
Hope you are using AlamofireUser.xcworkspace file to open the project.
Also all required CocoaPods libraries need to be installed into you system.
Regards,
Praveer
Yes.
I first installed CocoaPods (pod init; pod install to try on another project) and then I cloned your github project.
To open it I then closed Xcode and then, from terminal, open AlamofireUser.xcworkspace.
But I received the above errors.
I haven't found the podfile into your project. Is this normal?
Thanks and reagrds,
Luca
I added the Podfile and then I found that the problem was on SwiftyJSON.
To build correctly I cheanged it like this:
target 'AlamofireUser' do
# Comment the next line if you don't want to use dynamic frameworks
use_frameworks!
# Pods for AlamofireUser (iOS)
pod 'Alamofire', '~> 4.4'
pod 'SwiftyJSON','~> 4.0'
pod 'ODataSwift','~> 1.0'
end
Unfortunately now I received the following under gUserid:
Thread 1: Fatal error: Unexpectedly found nil while unwrapping an Optional value
this could be program side syntax error. Please check Constant and variable related file.
May be you need to make some correction.
Regards,
Praveer