• Skip to main content
  • Skip to primary sidebar

Ravi Shankar

Tweaking Apps

  • About
  • Portfolio
  • Privacy Policy

Archives for March 2015

payworks SDK integration in Swift

March 26, 2015 By Ravi Shankar Leave a Comment

payworks mPOS helps app developers to integrate their App with card reader. In this tutorial, we will see a sample app that integrates payworks mPOS using Swift.

Download the source code from github

Select New Project and choose Single View Application from the template.

201503260807.jpg

In the Project Options window, provide a product name and make sure to select Language as Swift. Click Next and Save the project.

201503260812.jpg

In the Project Navigator, select Main.storyboard and unmark check box “Use Size Classes” in the File Inspector. Then click Disable Size Classes button as this app is only designed for iPhone

201503260825.jpg

UI Design

Design the user interface as shown below.

  • Label to shown caption as Amount
  • TextField for entering the amount
  • Button to Charge Amount.
  • Label to display the status message.

201503260840.jpg

Just to reflect the purpose of this little app, let us rename the ViewController.Swift to ChargeViewController.swift. Make the corresponding changes to Class name as well in Identity Inspector.

Integrate mPOS SDK

We are going to add mPOS SDK to this project using CocoaPods by following the instructions here.

Close your Xcode project, launch Terminal window and and navigate to project folder.

201503260927.jpg

Create a new Podfile with the following statements

201503260929.jpg

Doing a pod install should download the mPOS SDK’s to your project folder.

201503260936.jpg

Now navigate to your project folder and open the file with extension as .xcworkspace. The Pods folder in the project navigator should contain the mPOS framework.

201503260956.jpg

Objective-C Bridging

We need to create a bridge file to call the objective-c related framework files in our Swift app. The easiest way to do this is to create new Objective-C file in your project.

Right click on your project folder, select New File.

201503260959.jpg

In the Choose a template screen, select Objective-C and click Next.

201503261005.jpg

Provide a name for the Objective-C file and save the file.

201503261007.jpg

Now you will prompted whether you would like to configure an Objective-C bridging header. Click Yes to create the bridging header file.

201503261007.jpg

As the header file is created, we do not need the temp objective-c file, you can delete this file.

201503261021.jpg

Navigate to Bridging-Header file in Project navigator and the following lines.

@import Foundation;

#import

Now you can make sure every thing works fine by doing a build. Also make sure to add the additional steps as mentioned in the instruction page for Miura Readers. Navigate to info.plist and add supported external accessory protocols and Required background modes keys.
201503261032.jpg
mPOS integration
Create two IBOutlets, one for TextField and another for Message Label.

  @IBOutlet weak var amountTxtField: UITextField!

@IBOutlet weak var messageLabel: UILabel!

  

Use the connection inspector to connect the IBOutlets with the controls.

201503261037.jpg

In order to connect with mPOS SDK, you need to register and get the merchant credentials, You can do this by registering here. After receiving the credentials create two constants to hold the identifier and secret key.

  let MERCHANT_IDENTIFIER = “YOUR_MERCHANT_IDENTIFIER”

let MERCHANT_SECRET_KEY = “YOUR_SECRET_KEY”

201503261049.jpg
Now add the following IBAction method to ChangeViewController.swift and connect the IBAction with the button.

  @IBAction func chargeCard(sender: UIButton) {

  

let amount:NSDecimalNumber = NSDecimalNumber(string: self.amountTxtField.text)

  

let transactionProvider:MPTransactionProvider = MPMpos .transactionProviderForMode( MPProviderMode.MOCK, merchantIdentifier: MERCHANT_IDENTIFIER, merchantSecretKey: MERCHANT_SECRET_KEY)

  

let template: MPTransactionTemplate = transactionProvider.chargeTransactionTemplateWithAmount(amount, currency: MPCurrency.EUR, subject: “subject”, customIdentifier: “customIdentifier”)

  

let paymentProcess:MPPaymentProcess = transactionProvider.startPaymentWithTemplate(template, usingAccessory: MPAccessoryFamily.Mock, registered: { (let paymentProcess:MPPaymentProcess!, let transaction:MPTransaction!) -> Void in

  

}, statusChanged: { (let paymentProcess:MPPaymentProcess!, let transaction:MPTransaction!, let paymentProcessDetails:MPPaymentProcessDetails!) -> Void in

  

self.messageLabel.text = self.formatMessage(paymentProcessDetails.information)

  

}, actionRequired: { (let paymentProcess:MPPaymentProcess!, let transaction:MPTransaction!, let transactionAction:MPTransactionAction, let transactionActionSupport:MPTransactionActionSupport!) -> Void in

  

}) {(let paymentProcess:MPPaymentProcess!, let transaction:MPTransaction!, let paymentProcessDetails:MPPaymentProcessDetails!) -> Void in

  

self.messageLabel.text = self.formatMessage(paymentProcessDetails.information)

}

}

  

func formatMessage(information:AnyObject) -> String {

let temp = (information[0] as NSString) + “\n”

return temp + (information[1] as NSString)

}

Since I don’t have a real reader to try this demo, I have used Mock mode for the transaction provider and payment process
  let transactionProvider:MPTransactionProvider = MPMpos .transactionProviderForMode( MPProviderMode.MOCK, merchantIdentifier: MERCHANT_IDENTIFIER, merchantSecretKey: MERCHANT_SECRET_KEY)

  let paymentProcess:MPPaymentProcess = transactionProvider.startPaymentWithTemplate(template, usingAccessory: MPAccessoryFamily.Mock, registered:

Now you are good to try this demo by entering an amount and tap the Pay button. The trisection status will be displayed in the message label.

201503261058.jpg
You can also test your solution by entering different amount as mentioned in the test page.
201503261101.jpg
Download the source code from github

Filed Under: Apple, iPhone, Mac, Programming Tagged With: Apple, integration, payowrks, Swift

Swift Interview Questions

March 7, 2015 By Ravi Shankar Leave a Comment

Listed below are some of topics to be familiar before attending an iOS//Swift interviews. These are very trivial questions but in future this space will updated with more scenario based questions

  • What are the different app states?
  • What is JSON?
  • What do you mean by REST?
  • Difference between class and struct?
  • What are Optionals?
  • Difference between Optional Chaining and Optional Binding?
  • why do we have to use UICollectionViewFlowLayout?
  • Difference between frame and bounds?
  • What is NSCoder class used for? 
  • What is NSOperationQueue and when should we use it?
  • What are extensions? Explain the difference between extension and subclassing?
  • Difference between delegates and NSNotifications?
  • What are protocols in Swift?
  • What is interpolation?
  • Explain type inference in Swift?
  • Difference between Auto Layout and Adaptive Layout?
  • Is possible to use an Objective-C framework in Swift? 
  • Explain the different life cycle of UIViewController?
  • Difference between xib’s and Storyboards.
  • Explain MVC?
  • What is MVVM?
  • what is an app bundle?
  • Write a sample code for Singleton design pattern?
  • What is difference between Unit tests and UI Test in Xcode?
  • Can you make updates to UI from a background thread? 
  • Difference between let and var keywords in Swift?
  • What are generics? Can you write generics example code?
  • Difference between designated and convenience initialiser?

Filed Under: Interview Questions, ios, Swift

Primary Sidebar

Recent Posts

  • We have blocked all requests from this device – Firebase Phone Authentication
  • iPhone is not available error message in Xcode
  • Clear CocoaPods cache, re-download and reinstall all pods
  • PDFKit – View, Annotate PDF file in Swift
  • Tab Bar Controller with WebView

Archives

  • September 2020
  • April 2020
  • December 2019
  • November 2019
  • October 2019
  • February 2019
  • October 2017
  • June 2017
  • May 2017
  • March 2017
  • September 2016
  • March 2016
  • February 2016
  • January 2016
  • December 2015
  • November 2015
  • October 2015
  • September 2015
  • August 2015
  • July 2015
  • June 2015
  • May 2015
  • April 2015
  • March 2015
  • February 2015
  • January 2015
  • December 2014
  • November 2014
  • October 2014
  • September 2014
  • August 2014
  • July 2014
  • June 2014
  • April 2014
  • March 2014
  • February 2014
  • January 2014
  • December 2013
  • October 2013
  • August 2013
  • July 2013
  • June 2013
  • April 2013
  • March 2013
  • February 2013
  • January 2013
  • November 2012
  • August 2012
  • July 2012
  • June 2012
  • May 2012
  • March 2012
  • February 2012
  • January 2012
  • December 2011
  • October 2011
  • September 2011
  • August 2011
  • July 2011
  • June 2011
  • April 2011
  • March 2011
  • January 2011
  • September 2010
  • August 2010
  • July 2010
  • June 2010
  • July 2009
  • March 2008

Copyright 2020 © rshankar.com